本文介绍了在MVC asp中注册多个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在MVC中映射多个具有不同顺序参数的路由:
I want to map several routes in MVC that have the parameters in different orders:
localhost:1010/abcd/home/index localhost:1010/home/index/abcd id=abcd controller=home action=index我尝试了下面的代码,但没有用.
I tried the code below, but it doesn't work.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "ShoppingManagment", "{id}/{controller}/{action}", new { controller = "ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }推荐答案
由于两个路由具有相同的格式,因此无法使用.
It will not work because both routes have the same format.
因此MVC路由引擎无法区分这两种网址格式.
So the MVC Routing Engine cannot differentiate between both the url patterns.
尝试将Controller直接写入url模式.
Try writing the Controller directly into the url pattern.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "ShoppingManagment", "{id}/ShoppingManagment/{action}", new { controller="ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }更多推荐
在MVC asp.net中注册多个路由
发布评论