Asp.Net MVC路由

一、普通路由

需要啓用屬性路由

App_Start/RouteConfig.cs

routes.MapMvcAttributeRoutes();//啓用路由屬性,啓用後可在控制器上設置路由

 

  1、修改Controller路由

[RoutePrefix("news")]                  //修改Controller路由,路由前綴(爲這個控制器設置一個統一的前綴,如果action不想應用可使用~去掉,例如[Route("~/test/tt")])
public class ProductController : Controller

  2、修改action路由

[Route("bs")]
public ActionResult Brand()

 

二、區域路由

1、默認路由

區域area:m

public class ProductController : Controller
    {
        public ActionResult Test()
        {
            return View();
        }
}

默認地址:/m/product/test

 

2、自定義路由

啓用屬性路由

context.Routes.MapMvcAttributeRoutes();//啓用路由屬性,啓用後可在控制器上設置路由

 

[RouteArea("m", AreaPrefix = "mm")]    //修改area路由
    [RoutePrefix("news")]                  //修改Controller路由
    [Route("{action}")]
    public class ProductController : Controller
    {
        [Route("tt")]
        public ActionResult Test()
        {
            return View();
        }
}

自定義路由後地址:/mm/news/tt

三、常見路由大全

https://www.cnblogs.com/weihengblogs/p/8876944.html

https://www.cnblogs.com/thestartdream/p/4246533.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章