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

posted @   WebApi  阅读(135)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2019-10-25 IIS添加MIME类型.woff/.svg/.woff2/.eot/.otf.ttf
CopyRight © 博客园 WebAPI
点击右上角即可分享
微信分享提示