2015-04-23 67 views
2

我定义我首页查看属性路由和RouteLink

<body> 
    <div> 
     <h1>Home</h1> 
     @Html.RouteLink("R1", "first", new { user="R1"}) 
     @Html.RouteLink("R2", "second", new { company = "R2" }) 
    </div> 
</body> 

登录控制器

public class LoginController : Controller 
    { 
     // GET: Login 

     [Route("{user}", Name = "first")] 
     public ActionResult Index(string user) 
     { 
      return View(); 
     } 
     [Route("{company}", Name = "second")] 
     public ActionResult Index2(string company) 
     { 
      return View(); 
     } 
    } 

RouteConfig

public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapMvcAttributeRoutes(); 
      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 


     } 
    } 

我想象在主页R1R2分别点击其路由正确的行为结果索引和索引2。

但其产生波纹错误

当前请求是采用以下的操作方法之间暧昧: System.Web.Mvc.ActionResult指数(System.String)上 类型MVCTest.Controllers.LoginController系统。 Web.Mvc.ActionResult 索引2(System.String)的类型MVCTest.Controllers.LoginController

我不知道为什么发生这种情况。

+0

我知道你忙的人,有一些问题'routes'考虑在寻找我的答案来解决你所面临的问题。 – adricadar

回答

1

问题:因为你只指定路线{params}请求将是www.example.con/params和程序是困惑,因为你有2与此URL操作,并这就是为什么错误。

www.example.con/matrixwebtech // Login/Index/matrixwebtech or Login/Index2/matrixwebtech? 
www.example.con/stackoverflow // Login/Index/stackoverflowor Login/Index2/stackoverflow? 

解决方案:正如你看到有这两条路线之间没有区别,我们必须做出一个。

解决这个你必须使路线不同name/{params}不仅{params}

public class LoginController : Controller 
{ 
    [Route("{user}", Name = "first")] 
    public ActionResult Index(string user) 
    { 
     return View(); 
    } 
    [Route("company/{company}", Name = "second")] 
    public ActionResult Index2(string company) 
    { 
     return View(); 
    } 
} 

现在要访问此,我们都会有这样的路线与没有confussion

www.example.con/matrixwebtech // /Login/Index/matrixwebtech 
www.example.con/company/stackoverflow // /Login/Index2/stackoverflow 

注:你可以找到更多信息关于Route here.

+0

嗨,昨天看了文章。并面临问题。请阅读上面的评论,我讨论nopcommerc。 – matrixwebtech

+0

@matrixwebtech我看着那个,但有**只有1个动作**只做1件事** **替代产品**。您尝试使用相同的地址访问** 2操作**,这可以与尝试访问www.stackoverlfow.com并期望看到Google相反...... – adricadar

+0

@matrixwebtech要做您所显示的内容,您只需** 1行动**在路线上'/'。你必须选择,看我的更新答案,我选择'用户'在'/'上。 – adricadar

1

而不是使用{用户}{公司},使用用户公司

例子:

[Route("user", Name = "first")] 
    public ActionResult Index(string user) 
    { 
     return View(); 
    } 
    [Route("company", Name = "second")] 
    public ActionResult Index2(string company) 
    { 
     return View(); 
    } 
+0

嗨,感谢您的回复 – matrixwebtech

+0

嗨,感谢您的回复,如果我使用'[Route(“user”,Name = “first”)]'url是**/user?user = R1 **,如果使用'[Route(“login/index/{user}”,Name =“first”)]'那么url是**/login/index/R1 **,但我不想要这样的网址。如果您在主页上的[link] http://demo.nopcommerce.com [/ link]左键点击** category菜单,请点击在**电子**上,并点击** 25美元虚拟礼品卡**产品的第一个URL是[链接] http://demo.nopcommerce.com/electronics [/链接]和第二[链接]演示。 nopcommerce.com/build-your-own-computer[\link],我认为他们也走向了不同的路线。如何做到这一点? – matrixwebtech

+0

那么你只想显示用户和公司?喜欢这个? localhost:1234/user/R1 –