1. 指定Controller、Action
Routes.MapRoute("","{controller}/{action}");
匹配:http://www.xxx.com/home/Index.
2.带默认参数指定
Routes.MapRoute("","{controller}/{action}",new {controller="Home",action="Index"});
匹配:http://www.xxx.com/home/Index.
http://www.xxx.com/home/
http://www.xxx.com/
3.静态参数和变量混用
Routes.MapRoute("","X{controller}/{action}");
匹配:http://www.xxx.com/Xhome/Index;
Routes.MapRoute("","X/{controller}/{action}");
匹配:http://www.xxx.com/X/home/Index.
4.不带controller
Routes.MapRoute("","X/{action}",new{controller="Home"});
匹配:http://www.xxx.com/X/Index.
5.自定义路由变量
Routes.MapRoute("","{controller}/{action}/{id}",new {controller="Home",action="Index",id="defaultId"});
自定义变量不能使用controller、action和area作为变量名。
6.访问路由变量的值
RouteData.Value[""]的值。
例如:ViewBag.V=RouteData.Value["action"];
另外一种方法就是把自定义变量当作Action的值传入到Action方法里面,如:
public ViewResult CustomVariable(string id) {
ViewBag.CustomVariable = index;
return View();
}
7.定义选择性URL变量
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
这样,URL里包含和不包含id变量的值都能通过编译。
8.通过C#选择性参数指定变量的默认值
public ViewResult CustomVariable(string id = "DefaultId") {
ViewBag.CustomVariable = id;
return View();
}
9.定义可变长度的路由变量
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
匹配:mydomain.com/Customer/List/All/Delete //catcall=Delete
mydomain.com/Customer/List/All/Delete/Perm //catcall=Delete/Perm
10. 包含多个相同名称的Controller
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "URLsAndRoutes.Controllers"});
通过指定controller的命名空间来区别两个名称相同的controller
可以通过指定告诉MVC Framework只在某一命名空间中寻找对应的Controller
Route myRoute = routes.MapRoute("AddContollerRoute", "Home/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "AdditionalControllers" });
myRoute.DataTokens["UseNamespaceFallback"] = false;
}
11.通过正则表达式限制路由
例如:
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*"},
new[] { "URLsAndRoutes.Controllers"});
就限制了controller必须是以H字母开头的
限制路由为特定的值:
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "^Index$|^About$"},
new[] { "URLsAndRoutes.Controllers"});
限制了路由为Index或About方法
限制路由的Http方法:
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "Index|About",
httpMethod = new HttpMethodConstraint("GET") },
new[] { "URLsAndRoutes.Controllers" });
12.从路由数据中构造URL
@Html.RouteLink("Routed Link",new{controller="Home",action="About",id="MyID"}); //效果同Action
@Url.RouteUrl(new{controller="Home",action="About",id="MyID"});//只产生URL,不带Action
13.在Action方法中产生URL
public ViewResult MyActionMethod() {
string myActionUrl = Url.Action("Index", new { id = "MyID" });
string myRouteUrl = Url.RouteUrl(new { controller = "Home", action = "Index" });
... do something with URLs...
}
14.重定向ActionResult
方法一:
public ActionResult MyActionMethod() {
return RedirectToAction("Index");
}
方法二:
public ActionResult MyOtherActionMethod() {
return RedirectToRoute(new { controller = "Home", action = "Index", id = "MyID" });
}
15.从特定路由中获取连接
routes.MapRoute("MyRoute", "{controller}/{action}");
routes.MapRoute("MyOtherRoute", "App/{action}", new { controller = "Home" });