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

微软资料

相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码1 天前
别再前后端各写一套表单校验了
java·后端
大勇前进1 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
yuzhi_liu1 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile1 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白801 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端
二月龙1 天前
PHP 接口返回统一响应封装,让前后端对接更省心
后端
盖伦发发1 天前
软件工程SOLID 五大设计原则
后端·软件工程