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

微软资料

相关推荐
hui函数3 分钟前
Flask-WTF表单验证全攻略
后端·python·flask·web·表单验证
喵手7 分钟前
Java异常处理最佳实践:如何避免捕获到不必要的异常?
java·后端·java ee
猿java19 分钟前
精通MySQL却不了解OLAP和 OLTP,正常吗?
java·后端·面试
喵手34 分钟前
Java中的HashMap:你了解它的工作原理和最佳实践吗?
java·后端·java ee
冷月半明1 小时前
把离线 Python 项目塞进 Docker:从 0 到 1 的踩坑实录,一口气讲透 10 个最常见困惑
后端
用户298698530141 小时前
如何使用 Spire.Doc 在 Word 中查找和替换文本?
后端
宫水三叶的刷题日记1 小时前
真的会玩,钉钉前脚辟谣高管凌晨巡查工位,小编随后深夜发文
前端·后端·面试
天天摸鱼的java工程师1 小时前
Go 语言未来会取代 Java 吗?
java·后端
野犬寒鸦1 小时前
力扣hot100:最大子数组和的两种高效方法:前缀和与Kadane算法(53)
java·后端·算法
AAA修煤气灶刘哥1 小时前
《从 0 到 1 上手:RBAC+SpringSecurity 权限管理教程》
java·后端·安全