1. 默认路由
路由就是URL到函数的映射。
比如:/Home/Index,这个url表示匹配Home控制器下的Index方法。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
2. 编写一条默认路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MyRoute",
url: "my/{controller}/{action}/{id}",
defaults: new { controller = "AdvFour", action = "EchartsTest1", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
3. 特性路由
开启特性路 :RouteTable.Routes.MapMvcAttributeRoutes();
[Route("RouteDemo/{name}/{age}")]
public ActionResult RouteTest(string name,int age)
{
return Content("特性路由"+name+ +age);
}
|