具有不同参数名称的多个参数的MVC路由

时间:2015-05-21 10:53:43

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

在我的家庭控制器中,我有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中进行更改。

我已经完成了这个

方法1

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

所以我把它改成了这样的

方法2

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

当我点击Url时

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

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

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

它让我异常。

这是解决方案。

正如Stephen Muecke所建议的

 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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

因为我给所有路线都给了同一个名字。和路由名称必须是唯一的,现在我已经重命名具有不同名称的路由。

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