排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
.NET Core
匹配单个路由
匹配固定的单个路径
endpoints.MapControllerRoute( name: "AA", pattern: "About", defaults: new { controller = "About", action = "Index" });
action和controller交换顺序
action和controller交换顺序,自定义路由模块如下 把两个顺序互换即可。
endpoints.MapControllerRoute( name: "BB", pattern: "{action=Index}/{controller=Home}/{id?}");
只限制控制器
因为只能输入控制器,action不能输入,所以action只能是默认的index,action恒等于index如果你输入的是:/home,其实访问的就是home/index(默认的)如果你输入的是:/news,其实访问的就是news/index(默认的)
endpoints.MapControllerRoute( name: "AA", pattern: "{controller=Home}", defaults: new { controller = "Home", action = "Index" });
自定义地址规则
控制器就是死的,控制器只能是home,不是mylove,mylove只是我自己写死的路由规则。
必须满足自己定义的规则
endpoints.MapControllerRoute( name: "AA", pattern: "mylove/{action}", defaults: new { controller = "Home", action = "Index" });
上面几个定义完整点的代码
app.UseEndpoints(endpoints => { //匹配单个路由 endpoints.MapControllerRoute( name: "AA", pattern: "About", defaults: new { controller = "About", action = "Index" }); //只限制控制器 endpoints.MapControllerRoute( name: "AA", pattern: "{controller=Home}", defaults: new { controller = "Home", action = "Index" }); //自定义地址规则 endpoints.MapControllerRoute( name: "AA", pattern: "mylove/{action}", defaults: new { controller = "Home", action = "Index" }); //action和controller交换顺序 endpoints.MapControllerRoute( name: "BB", pattern: "{action=Index}/{controller=Home}/{id?}"); //默认路由 endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
路由约束
约束路由参数必须是整形,bool,时间,小数,字符串限制等等
//约束参数id必须是整数 endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id:int?}"); //约束active参数必须是bool值 endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{active:bool?}"); //路由约束 - 匹配decimal endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{price:decimal?}"); //路由约束 - 匹配字符串长度 endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{username:minlength(4?}");
路由约束使用正则表达式
代码如下:
endpoints.MapControllerRoute( name: "people", pattern: "People/{ssn}", constraints: new { ssn = "^\\d{3}-\\d{2}-\\d{4}$", }, defaults: new { controller = "People", action = "List", });
能正确匹配的格式如下:
其他一些自定义路由
endpoints.MapControllerRoute( name: "UserCategory", pattern: "{UserName?}/AA/Category/{id?}", defaults: new { controller = "Category", action = "Category" } ); endpoints.MapControllerRoute( name: "Show", pattern: "{UserName?}/AA/BB/{id?}", defaults: new { controller = "ArticleDetails", action = "Index" } ); endpoints.MapControllerRoute( name: "Resource", pattern: "Resource/Show/{UserName?}/{id?}", defaults: new { controller = "Resource", action = "Show" } ); endpoints.MapControllerRoute( name: "Search", pattern: "{UserName?}/search", defaults: new { controller = "Search", action = "UserSearch" }
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价