ASP.NET Core 路由规则,自定义特性路由 ,IActionConstraint 路由约束 总结 mvc

资料

资料

路由服务

路由服务是在 Program.cs 中使用 builder.Services.AddRouting()注册的,

只是默认在 builder 之前已经注册过了,无需我们再次注册。

AddRouting()方法必须在 UseRouting()方法之前运行,它是路由的基础服务。

MapControllerRoute 用于常规路由

MapDefautControllerRoute 用于常规路由 这是上面的内容,但它简写了我上面显示的默认模式的配置。

MapControllers 属性路由 【特性路由】 最常用于 restfull api 或者webapi,

UseMvc 不能跟终结点路由一起使用 支持多路由 支持特性路由

//不启用终结点

builder.services.AddMvc(options => options.EnableEndpointRouting = false);

app.UseMvc(routes =>

{

routes.MapRoute(

name: "default",

template: "{controller=Home}/{action=Index}/{id?}");

routes.MapRoute(

name: "defaultXX",

template: "Test/{controller=xxx}/{action=Index}/{id?}");

});

UseMvcWithDedaultRoute 是app.UseMvc 的默认值 简写

使用名为"default"的默认路由和以下模板将 MVC 添加到 IApplicationBuilder

请求执行管道:"{controller=Home}/{action=Index}/{id?}"。

UseRouting UseEndpoints 终结点路由 支持多路由

UseRouting() 方法用于配置请求路由。

UseEndpoints() 方法用于定义请求的服务端点。

》》》配置终结点委托

MapGet

MapPost

MapPut

MapDelete

MapControllerRoute

MapHealthChecks

其他类似"MapXXX"的方法

总结

路由一共2种 UseEndpoints 和 UseMvc 不能同时使用
资料

自定义特性路由

比如HttpGet 继承 HttpMethodAttribute

csharp 复制代码
public class MyApiControllerAttribute:Attribute,IRouteTemplateProvider
{
	public string Template => "api/[controller]";
	public int?Order {get;set}
	public string Name {get;set;}
}
IActionConstraint 路由约束

实现IActionConstraint最简单的方法是创建派生自 System.Attribute 的类,并将其置于操作和控制器 上。

MVC 将自动发现任何应用属性IActionConstraint的操作和控制器

csharp 复制代码
//定义ActionConstraint属性约束
public class CountrySpecificAttribute : Attribute, IActionConstraint
    {
        private readonly string _countryCode;
 
        public CountrySpecificAttribute(string countryCode)
        {
            _countryCode = countryCode;
        }
        //  越小 优先级越高
        public int Order
        {
            get
            {
                return 0;
            }
        }
 
        public bool Accept(ActionConstraintContext context)
        {
            return string.Equals(
                context.RouteContext.RouteData.Values["languagecode"].ToString(),
                _countryCode,
                StringComparison.OrdinalIgnoreCase);
        }
    }
csharp 复制代码
      //应用路由的action约束,并且路由中id值为en-US
       [CountrySpecific("en-US")]
        public IActionResult Privacy(string countryCode)
        {
            return View();
        }
//在浏览器测试时:如果输入http://localhost:30081/home/Privacy/zh-cn,则网页显示404。
//如果输入http://localhost:30081/home/Privacy/en-US 则符合约束,网页显示正常。
//路由是  
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{languagecode}");        
});

微软资料

相关推荐
IT_陈寒17 分钟前
Java 性能优化:5个被低估的JVM参数让你的应用吞吐量提升50%
前端·人工智能·后端
南囝coding29 分钟前
《独立开发者精选工具》第 018 期
前端·后端
绝无仅有38 分钟前
数据库MySQL 面试之死锁与排查经验总结
后端·面试·github
用户384958730691 小时前
Spring Boot 集成 Redis 的完整流程
后端
昨日的风1 小时前
springboot 多数据源切换
后端
绝无仅有2 小时前
mysql性能优化实战与总结
后端·面试·github
用户8356290780512 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
德育处主任2 小时前
玩转 Strands:AI Agent 开发,原来可以这么简单!
后端·aigc
Undoom2 小时前
大模型选型“炼狱”与终结:一份来自普通开发者的AI Ping深度评测报告
后端
用户4099322502122 小时前
FastAPI的CI流水线怎么自动测端点,还能让Allure报告美到犯规?
后端·ai编程·trae