[ASP.NET Core] Middleware
来源:互联网 发布:vscode自动补全 编辑:程序博客网 时间:2024/05/18 17:01
前言
本篇文章介绍ASP.NET Core里,用来处理HTTP封包的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员。
ASP.NET Core官网
结构
在ASP.NET Core里,每个从「浏览器传入」的HTTP Request封包,会被系统封装为「HttpRequest对象」,并且配置默认的HttpResponse对象、Session对象、ClaimsPrincipal对象...等等物件。接着将这些对象,封装成为一个「HttpContext对象」,用来提供ASP.NET Core后续使用。
ASP.NET Core在收到HttpContext之后,会把它交给一个「Pipeline」去处理。这个Pipeline里面配置很多「Middleware」。系统会将HttpContext,依序传递给Pipeline里的Middleware去处理。每个Middleware会依照自己内部的程序逻辑,来运算处理HttpContext,并且变更HttpContext所封装的对象内容。
ASP.NET Core在收到经由Middleware处理完毕的HttpContext之后,就会取出其中所封装的HttpResponse对象。然后依照这个HttpResponse对象,来建立从「服务器回传」的HTTP Response封包内容。
ASP.NET Core经由上述的系统结构,完成HTTP Request封包输入、HTTP Response封包输出的工作流程。
开发
Invoke
在[ASP.NET Core] Getting Started这篇文章里,提供了一个ASP.NET Core的Middleware范例:HelloWorldMiddleware。在这个范例里,Middleware透过实做Invoke方法,来提供自己所封装的程序逻辑。
public class HelloWorldMiddleware{ // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Response context.Response.WriteAsync("Hello World!"); // Return return Task.CompletedTask; }}
HttpContext.Request
在实做Middleware.Invoke方法的时候,开发人员可以透过HttpContext.Request,来取得从「浏览器传入」的HTTP Request封包内容。在下列的范例程序代码里,就是透过HttpContext.Request的Path、QueryString两个属性,来分别取得HTTP Request封包的URL路径与QueryString内容。
public class HelloWorldMiddleware{ // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Request string path = context.Request.Path.ToString(); string queryString = context.Request.QueryString.ToString(); string message = string.Format("path={0}, queryString={1}", path, queryString); // Response context.Response.WriteAsync(message); // Return return Task.CompletedTask; }}
HttpContext.Response
同样在实做Middleware.Invoke方法的时候,开发人员可以透过HttpContext.Response,来设定从「服务器回传」的HTTP Response封包内容。在下列的范例程序代码里,就是透过HttpContext.Response的WriteAsync方法、StatusCode属性,来分别设定HTTP Response封包的Content与StatusCode。
public class HelloWorldMiddleware{ // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Response context.Response.StatusCode = 404; context.Response.WriteAsync("Not Found"); // Return return Task.CompletedTask; }}
Exception
而在实做Middleware.Invoke方法的时候,如果程序代码里发生了预期之外的Exception。ASP.NET Core预设会使用「500 Internal Server Error」,这个StatusCode来通报系统内部发生异常。 在下列的范例程序代码里,就是直接抛出一个例外错误,交由ASP.NET Core的错误处理机制去处理。
public class HelloWorldMiddleware{ // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Exception throw new Exception(); // Return return Task.CompletedTask; }}
RequestDelegate
建立Middleware的时候,开发人员可以透过建构子所传入的RequestDelegate,来参考到Pipeline里的下一个Middleware。透过调用RequestDelegate,就可以调用Pipeline里的下一个Middleware的Invoke方法。在下列的范例程序代码里,就是透过调用RequestDelegate,来调用Pipeline里的下一个Middleware的Invoke方法,藉此串接其他Middleware的程序逻辑。
public class HelloWorldMiddleware{ // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public async Task Invoke(HttpContext context) { // Do Something 01 //.... // Next await _next.Invoke(context); // Do Something 02 // ... }}
参考
Middleware - ASP.NET Core
ASP.NET Core 的 Middleware - ASP.NET Core 信息分享
相关文章:
Middleware的艺术
dotnetCore增加MiddleWare的Run,Use Map MapThen四个扩展方法
ASP.NET Core提供模块化Middleware组件
ASP.NET Core 开发-中间件(Middleware)
[ASP.NET Core] Static File Middleware
用Middleware给ASP.NET Core Web API添加自己的授权验证
原文地址:http://www.cnblogs.com/clark159/p/5974280.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
- [ASP.NET Core] Middleware
- [ASP.NET Core] Middleware
- [ASP.NET Core] Static File Middleware
- ASP.NET Core 开发-中间件(Middleware)
- .NET Core 中间件MiddleWare
- Asp.Net Core Authentication Middleware And Generate Token
- ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析
- ASP.NET Core轻松入门之Middleware管道模型
- ASP.NET Core 运行原理解剖[3]:Middleware-请求管道的构成
- ASP.NET Core 运行原理解剖[3]-Middleware-请求管道的构成
- ASP.NET Core 运行原理解剖[3]-Middleware-请求管道的构成
- ASP.NET Core 运行原理解剖[3]:Middleware-请求管道的构成
- 用Middleware给ASP.NET Core Web API添加自己的授权验证
- ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
- 解读ASP.NET 5 & MVC6系列(6):Middleware详解
- ASP.NET Core 介绍
- ASP.NET Core
- Asp.net Core demo
- .NET Core项目从xproj+project.json向csproj迁移简介
- 表单验证
- 广东省强网杯CTF Web部分详解
- 老司机实战Windows Server Docker:1 初体验之各种填坑
- 跨平台的.NET邮件协议MailKit组件解析
- [ASP.NET Core] Middleware
- OAuth2.0 知多少
- 使用 Angular
- .Net Core迁移到MSBuild平台
- unity开发htc VR实现瞬移功能
- 从Visual Studio看微软20年技术变迁
- 当我们在谈大前端的时候,我们谈的是什么
- .Net Core迁移到MSBuild的多平台编译问题
- 使用 C# 运行符号测试