【问题标题】:Attribute routing and RouteLink属性路由和RouteLink
【发布时间】:2015-04-23 07:36:08
【问题描述】:

我定义了我的主视图,比如

<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();
      }
   }

路由配置

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 上分别点击其路由到正确的操作结果 Index 和 Index2。

但它会产生以下错误

当前请求在以下操作方法之间存在歧义: System.Web.Mvc.ActionResult Index(System.String) 类型 MVCTest.Controllers.LoginController System.Web.Mvc.ActionResult MVCTest.Controllers.LoginController 类型上的 Index2(System.String)

我不知道为什么会这样。

【问题讨论】:

  • 我知道您很忙,并且对routes 有一些问题,请考虑查看我的答案以解决您面临的问题。

标签: c# asp.net-mvc


【解决方案1】:

不要使用 {user}{company},而是使用 usercompany

例子:

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

【讨论】:

  • 您好,感谢您的回复,如果我使用 [Route("user", Name = "first")],则 url 是 /user?user=R1 并且如果使用 ` [Route("login/index/{user}" , Name = "first")] `那么 url 是 /login/index/R1 但我不想要这样的 url。如果你在主页上转到 [link]demo.nopcommerce.com[/link]左侧 类别 菜单点击 Electronics ** 并点击 **$25 Virtual Gift Card 产品,第一个网址是 [link]demo.nopcommerce.com/electronics[/link],第二个 [link] demo.nopcommerce.com/build-your-own-computer[\link] ,我认为他们也路由到不同的路由。如何做到这一点?
  • 所以您只想显示用户和公司?像这样?本地主机:1234/user/R1
  • localhost:1234/user/R1 这很好,但我不知道这是否可能?我可以显示 localhost:1234/R1 还是一些东西,程序会知道要调用哪个动作。你可以看到 nopcommece 网站。
  • 是的,这是可能的。但是您需要添加额外的(在路由配置中更具体的路由)
  • 所以它不是由属性路由完成的,我必须通过基于对流的路由来完成。对吧?你能给我一些想法吗?我该怎么做,一个示例或教程链接不胜感激。
【解决方案2】:

问题:因为您只在路由中指定{params},所以请求将发送到www.example.con/params,程序混淆,因为您有2个操作 使用此网址,这就是错误的原因。

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();
    }
}

现在要访问它,我们将拥有这条没有混淆

的路线
www.example.con/matrixwebtech // /Login/Index/matrixwebtech
www.example.con/company/stackoverflow // /Login/Index2/stackoverflow

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

【讨论】:

  • 嗨,昨天阅读了这篇文章。并面临问题。请阅读上面我讨论 nopcommerc 的 cmets。
  • @matrixwebtech 我看过了,但是只有 1 个动作只做一件事 displaing a product。您尝试使用相同的地址访问 2 个操作,这可以与尝试访问 www.stackoverlfow.com 并期望看到 Google 进行比较...
  • @matrixwebtech 要执行您所展示的操作,您只需只需执行 1 个操作即可在路线 / 上。您必须选择,请参阅我的更新答案,我选择user/ 上。
  • 这件事我不是很清楚,所以它不是通过属性路由完成的,我必须通过基于对流的路由来完成。对吗?你能给我一些想法吗?我该怎么做,一个示例或教程链接不胜感激
  • @matrixwebtech 你想要什么叫做 slug url 你可以在整个互联网上找到这个,它可以通过属性路由或没有它们来完成,这并不重要.行动是最重要的。 See here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2016-12-17
  • 2014-08-27
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多