MVC routing with multiple parameter with different parameter name
10,364
Solution 1
As I had given same name to all route. and route name must be unique, now i have rename the route with different name.
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 }
);
Solution 2
Route name should be unique, You have two routes with same name: "Home"
Comments
-
Amit Kumar almost 2 years
In my home controller, I have 3 Action methods. which is give below.
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(); }
As I had given diffrent parameter name in each action methods so I have to change in
Route.config
.I have Done this
Method 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 } );
but it is giving me exception like
Home' is already in the route collection. Route names must be unique
So I have change it to like this
Method 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 } );
When I hit Url like
http://localhost:50958/Home/Index/2
http://localhost:50958/Home/Index2/2/3
http://localhost:50958/Home/Index3/2/3/4
it throwing me exception.
This is Solution.
As suggested by 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 } );