.Net Core 路由处理
路由基础知识
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
终结点
- 用于 Razor Pages 的 MapRazorPages
- 用于控制器的 MapControllers
- 用于 SignalR 的 MapHub
- 用于 gRPC 的 MapGrpcService
app.UseRouting(); app.UseAuthentication();
app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/healthz").RequireAuthorization();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
终结点元数据
- 可以通过路由感知中间件来处理元数据。
- 元数据可以是任意的 .NET 类型。
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.Use(next => context =>
{
var endpoint = context.GetEndpoint();
if (endpoint?.Metadata.GetMetadata<AuditPolicyAttribute>()?.NeedsAudit ==true)
{
Console.WriteLine("开始处理事务逻辑");
Console.WriteLine($"ACCESS TO SENSITIVE DATA AT: {DateTime.UtcNow}");
}
return next(context);
}); app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello world!");
}); // Using metadata to configure the audit policy.
endpoints.MapGet("/sensitive", async context =>
{
await context.Response.WriteAsync($"sensitive data{DateTime.UtcNow}");
})
.WithMetadata(new AuditPolicyAttribute(needsAudit: true));
});
}
} public class AuditPolicyAttribute : Attribute
{
public AuditPolicyAttribute(bool needsAudit)
{
NeedsAudit = needsAudit;
} public bool NeedsAudit { get; }
}
比较终端中间件和路由
- 这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
- 中间件通过返回而不是调用 next 来终止管道。
- 终结点始终是终端。
终端中间件允许在管道中的任意位置放置中间件:
- 终结点在 UseEndpoints 位置执行。
- 终端中间件允许任意代码确定中间件匹配的时间:
- 自定义路由匹配代码可能比较复杂,且难以正确编写。
- 路由为典型应用提供了简单的解决方案。
- 大多数应用不需要自定义路由匹配代码。
- 带有中间件的终结点接口,如 UseAuthorization 和 UseCors。
- 通过 UseAuthorization 或 UseCors 使用终端中间件需要与授权系统进行手动交互
- 这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
设置传统路由
app.UseEndpoints(endpoints =>
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
});
REST Api 的属性路由
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
- Route[]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{ [Route("Index")]
public string Index(int? id)
{
return "Test";
}
}
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
{
public string Index(int? id)
{
return "Test";
}
}
[ApiController]
public class WeatherForecastController : ControllerBase
{
[Route("[controller]/Index")]
public string Index(int? id)
{
return "Test";
}
}
- Http[Verb]
[ApiController]
public class WeatherForecastController : ControllerBase
{
[HttpGet("[controller]/Index")]
public string Index(int? id)
{
return "Test";
}
}
- Route[]和Http[Verb]混合使用
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{ [HttpGet("Index")]
public string Index(int? id)
{
return "Test";
}
}
.Net Core 路由处理的更多相关文章
- ASP .Net Core路由(Route) - 纸壳CMS的关键
关于纸壳CMS 纸壳CMS是一个开源免费的,可视化设计,在线编辑的内容管理系统.基于ASP .Net Core开发,插件式设计: GitHub:https://github.com/SeriaWei/ ...
- 构建可读性更高的 ASP.NET Core 路由
原文:构建可读性更高的 ASP.NET Core 路由 一.前言 不知你在平时上网时有没有注意到,绝大多数网站的 URL 地址都是小写的英文字母,而我们使用 .NET/.NET Core MVC 开发 ...
- ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 路由 前两章节中,我们提到 ASP.NET Core 支持 MVC 开发 ...
- ASP.NET Core路由中间件[3]: 终结点(Endpoint)
到目前为止,ASP.NET Core提供了两种不同的路由解决方案.传统的路由系统以IRouter对象为核心,我们姑且将其称为IRouter路由.本章介绍的是最早发布于ASP.NET Core 2.2中 ...
- ASP.NET Core路由中间件[2]: 路由模式
一个Web应用本质上体现为一组终结点的集合.终结点则体现为一个暴露在网络中可供外界采用HTTP协议调用的服务,路由的作用就是建立一个请求URL模式与对应终结点之间的映射关系.借助这个映射关系,客户端可 ...
- .Net core路由高级用法
先说startup中的路由 这里是我们现在用的默认路由,但是在使用当中也有麻烦.总而言之 用的不爽. 使用属性路由:RouteAttribute特性 默认的HomeController下面的Index ...
- ASP.NET Core路由中间件[1]: 终结点与URL的映射
目录 一.路由注册 二.设置内联约束 三.默认路由参数 四.特殊的路由参数 借助路由系统提供的请求URL模式与对应终结点(Endpoint)之间的映射关系,我们可以将具有相同URL模式的请求分发给应用 ...
- 理解ASP.NET Core - 路由(Routing)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 Routing Routing(路由):更准确的应该叫做Endpoint Routing,负责 ...
- Net core学习系列(六)——Net Core路由
一.概述 路由主要有两个主要功能: 1.将请求的URL与已定义的路由进行匹配,找到该URL对应的处理程序并传入该请求进行处理. 2.根据已定义的路由生成URL 这两个功能看起来这两个是相反的. A.路 ...
- core路由设置
全局路由设置 app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exi ...
随机推荐
- GitHub & GitHub Package Registry
GitHub & GitHub Package Registry npm https://github.blog/2019-05-10-introducing-github-package-r ...
- 为什么建议选择居住在墨尔本CBD以南2公里内
英国房地产公司PAUL ADAMS ARCHITECT ,简称PPA(公司编号:07635831)成立于2011年,是一家成立近十年的老牌房地产公司.PAA公司一直有着自己的房地产理念,秉持房子是用来 ...
- ffmpeg中的日志控制、保存日志到指定文件、处理进度查询
ffmpeg默认就会输出一些日志,包含编译的工具(gcc版本).可用的组件(--enable-xxxx).组件版本号码,如下图 这些信息是默认输出的,每次执行合作都会打印在控制台中,如果不想看这些信息 ...
- (转)linux下的系统调用函数到内核函数的追踪
转载网址:http://blog.csdn.net/maochengtao/article/details/23598433 使用的 glibc : glibc-2.17使用的 linux kerne ...
- 使用dlopen加载动态库
目录 概述 接口 C CMakeLists.txt src/main.c src/add.c ./dlopen_test C++ CMakeLists.txt src/main.cpp src/add ...
- 一个基于 Vue3 的开源项目,3个月时间 star 终于破千!
本文主要是对如何做开源项目的一些思考. 前文回顾: <Vue3 来了,Vue3 开源商城项目重构计划正式启动!> <一个基于 Vue 3 + Vant 3 的开源商城项目> 关 ...
- 《C++ Primer》笔记 第12章 动态内存
shared_ptr和unique_ptr都支持的操作 解释 shared_ptr sp或unique_ptr up 空智能指针,可以指向类型为T的对象 p 将p用作一个条件判断,若p指向一个对象,则 ...
- CCF(除法):线段树区间修改(50分)+线段树点修改(100分)+线段树(100分)
除法 201709-5 这道题有很多种方法来做,最常用的就是线段树和树状数组. 如果使用线段树来做,就会想到区间修改的update函数.但是这里可能会涉及到v是1或者a[j]是0的情况,所以用这种方法 ...
- HDOJ-6665(离散化+DFS求连通分量)
Calabash and Landlord HDOJ-6665 这里考察的是离散化的知识. 首先将所有的x坐标和y坐标放入两个数组中,然后对这两个数组进行排序.因为总共的坐标数就5个所以这两个数组的大 ...
- pytorch(02)tensor的概念以及创建
二.张量的简介与创建 2.1张量的概念 张量的概念:Tensor 张量是一个多维数组,它是标量.向量.矩阵的高维拓展 Tensor与Variable Variable是torch.autograd(t ...