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}");        
});

微软资料

相关推荐
无人不xiao17 小时前
springBoot 实现 接口进度条
java·spring boot·后端
IT_陈寒17 小时前
Redis缓存击穿把我坑惨了,原来这样解决才靠谱
前端·人工智能·后端
Lee川17 小时前
RAG 知识库问答:从概念到代码的完整实现
前端·人工智能·后端
咪的Coding18 小时前
为什么在 DeepSeek 输入 <think>,它竟吐出别人的“记忆碎片”!?
后端·deepseek
HLAIA光子18 小时前
这些Spring Boot写法已经过时了!
spring boot·后端
alwaysrun18 小时前
Zig实现Windows下进程监控
后端·编程语言
Nyarlathotep011318 小时前
定时线程池:ScheduledThreadPoolExecutor
java·后端
逍遥德19 小时前
SpringBoot自带TaskScheduler 接口实现定时任务的动态增、删、启、停。
java·spring boot·后端·中间件
jieyucx20 小时前
Go 语言核心关键字:defer 深度解析与实战避坑
开发语言·后端·golang·defer