Route
程序员文章站
2022-06-02 18:35:20
...
- General
routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}");
匹配:
http://mydomain.com/Admin/Index
http://mydomain.com/Home/Index (默认)
- Static URL Segments
routes.MapRoute(name: "", template: "Public/{controller=Home}/{action=Index}");
匹配:
http://mydomain.com/Public/Admin/Index
http://mydomain.com/Public/Home/Index (默认)
- Mixed
routes.MapRoute("", "X{controller}/{action}");
匹配:
http://mydomain.com/XAdmin/Index
http://mydomain.com/XHome/Index
应用:
routes.MapRoute("", "Shop/OldAction", new { controller = "Home", action = "Index" });
会匹配到
http://mydomain.com/Home/Index
- 自定义Segment变量
routes.MapRoute("", "{controller=Home}/{action=Index}/{id=DefaultId}");
Note: controller,action和area为保留字
- 可选Segment
routes.MapRoute("", "{controller=Home}/{action=Index}/{id?}");
- catchall
routes.MapRoute("", "{controller=Home}/{action=Index}/{id?}/{*catchall}");
匹配:
http://mydomain.com/Home/List/123/123/12/31/2312/3
其中catchall为"123/12/31/2312/3"
- 约束Routes
routes.MapRoute("", "{controller=Home}/{action=Index}/{id:int?}");
匹配:
http://mydomain.com/CustomVariable/Hello/1
- Attribute Routing
public class CustomerController : Controller
{
[Route("myroute")]
public ViewResult Index() => View("Result", new Result { Controller = nameof(CustomerController), Action = nameof(Index) });
public ViewResult List() => View("Result", new Result { Controller = nameof(CustomerController), Action = nameof(Index) });
}
匹配:
http://mydomain.com/myroute
且无法通过如下Url访问:
http://mydomain.com/Customer/Index
可以添加[controller]:
public class CustomerController : Controller
{
[Route("[controller]/MyAction")]
public ViewResult Index() => View("Result", new Result { Controller = nameof(CustomerController), Action = nameof(Index) });
public ViewResult List() => View("Result", new Result { Controller = nameof(CustomerController), Action = nameof(Index) });
}
匹配:
http://mydomain.com/Customer/MyAction
上一篇: route
下一篇: 基于关系型数据库认证
推荐阅读
-
Linux route命令详解和使用示例(查看和操作IP路由表)
-
探讨AngularJs中ui.route的简单应用
-
关于使用 symfony 3.4.32中Luckynumber 页面的 route 书写
-
详解CentOS中的route命令
-
AngularJS通过ng-route实现基本的路由功能实例详解
-
AngularJS基于ui-route实现深层路由的方法【路由嵌套】
-
使用vue-route 的 beforeEach 实现导航守卫(路由跳转前验证登录)功能
-
Vue组件和Route的生命周期实例详解
-
Python的Flask框架中@app.route的用法教程
-
远程连接mysql很慢,但是本地连接mysql却很快,ping和route网络通信都是正常的怎么办?