【问题标题】:MVC routing with multiple parameter with different parameter name具有不同参数名称的多个参数的 MVC 路由
【发布时间】:2015-05-21 10:53:43
【问题描述】:

在我的家庭控制器中,我有 3 个 Action 方法。 这是下面给出的。

        public ActionResult Index(int id)
        {
            return View();
        }


        public ActionResult Index2(int did,int docType)
        {
            return View();
        }


        public ActionResult Index3(int uid,int docId,int typeId)
        {
            return View();
        }

由于我在每个操作方法中给出了不同的参数名称,所以我必须更改 Route.config

我已经完成了

方法一

routes.MapRoute(
                name: "Home",
                url: "{controller}/{action}/{did}/{docType}",
                defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Home",
                url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

但它给了我例外

Home' is already in the route collection. Route names must be unique

所以我把它改成这样了

方法二

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

当我点击网址时

http://localhost:50958/Home/Index/2

http://localhost:50958/Home/Index2/2/3

http://localhost:50958/Home/Index3/2/3/4

它给我抛出异常。

这是解决方案。

按照斯蒂芬·穆克的建议

 routes.MapRoute(
                name: "Admin",
                url: "{controller}/{action}/{did}/{docType}",
                defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "User",
                url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

【问题讨论】:

  • 第一个错误的原因是因为您的前 2 个路由定义具有 name: "Home", - 它们必须是唯一的。换一个就行了
  • @StephenMuecke:但我所有的动作都在 Home Controller 中。
  • 这与它毫无关系。然后打电话给你想要的任何东西。他们只需要独一无二
  • @StephenMuecke:谢谢。有用。方法2可以吗。
  • 我对此表示怀疑。只有最后一个参数可以是可选的,因此您每次都需要提供至少 5 个参数以避免歧义。还要注意 name 属性由诸如 @Html.RouteLink("XYZ"); 之类的方法使用,它匹配 routes.MapRoute( name: "XYZ, ...)

标签: c# asp.net-mvc asp.net-mvc-4 routing


【解决方案1】:

路线名称应该是唯一的,您有两条同名路线:“Home”

【讨论】:

    【解决方案2】:

    因为我给所有路线都起了相同的名字。并且路线名称必须是唯一的,现在我已经用不同的名称重命名了路线。

    routes.MapRoute(
                    name: "Admin",
                    url: "{controller}/{action}/{did}/{docType}",
                    defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
                );
                routes.MapRoute(
                    name: "User",
                    url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                    defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
                );
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
    

    【讨论】:

      猜你喜欢
      • 2017-05-27
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      相关资源
      最近更新 更多