List
Foreword
本文介绍Asp.Net Core 中间件。
surroundings
1.Visual Studio 2017
2.Asp.Net Core 2.2
Start
Firsthand: Middleware Overview
1.中间件:添加到应用管道用于处理处理请求和响应的组件。每个组件:
- 可以选择是否将请求传递到管道中的下一个组件;
- 可以在调用管道中的下一个组件之前和之后执行工作;
2.管道:如图中黑色箭头,从请求到相应的过程,称之为管道;
3.上图解释:当Request进来时,首先进入Middleware1,然后调用next方法进入Middleware2,
然后调用next方法进入Middleware3,没有next方法所以再依次返回,完成管道处理; 4.三种方法添加中间件: - Use:通常使用Use,Use也可使管道短路,即不调用next方法; - Run:Run一般在管道末端使用Run方法; - Map:Map一般用于处理特定的请求路径。 下面介绍这三种方法的使用。
Second hand: the use of middleware
1.Use of use
1.说一下IApplicationBuilder,Use、Run、和Map添加中间件,其实都是添加到IApplicationBuilder
中的一个组件容器内,在执行时,会遍历这个组件容器,依次执行中间件。
2.Run use
3.Map use
Built-in middleware
Asp.Net Core内置了以下中间件(用法:Use{中间件}):
Middleware | description | order |
---|---|---|
Authentication | It provides authentication support. | We needed HttpContext.User before. OAuth called back to the terminal. |
Cookie Policy | Whether tracking user consent to store personal information, and enforce the cookie field (such as secure and SameSite ) minimum standards. |
Before issuing the cookie middleware. Example: authentication, session, MVC (TempData). |
HEARTS | Configuring cross-domain resource sharing. | Before using components of CORS. |
Diagnostics | Configure diagnostic. | Before generating the error components. |
Forwarded Headers | The proxy header forwarding to the current request. | Before using updated components field. Example: the program, the host, the IP client, method. |
Health Check | Check the application and its dependencies ASP.NET Core operating conditions, such as checking database availability. | If the request matches the endpoint checks operating conditions, for the terminal. |
HTTP Method Override | Override allows incoming POST request method. | Before using components have been updated methods. |
HTTPS Redirection | Will redirect all HTTP requests to HTTPS (ASP.NET Core 2.1 or later). | Before using components of the URL. |
HTTP Strict Transport Security (HSTS) | Add special security header in response to enhanced middleware (ASP.NET Core 2.1 or later). | Before sending the response, after the modification request component. Example: adapter, URL rewriting. |
MVC | Processing request (ASP.NET Core Version 2.0 or later) with MVC / Razor Pages. | If the request matches the route, for the terminal. |
OWIN | Interoperable OWIN based applications, and middleware servers. | If the request is processed OWIN middleware, for the terminal. |
Response Caching | It provides support for caching the response. | Prior to cache components. |
Response Compression | Provide support for compressed response. | Before compression required components. |
Request Localization | Provide localized support. | Prior to localize sensitive components. |
Routing | Request routing definitions and constraints. | Terminal for the matching route. |
Session | It provides support for managing user sessions. | Before the components needed sessions. |
Static Files | To provide support static files and directories browsing. | If the request matches the file, for the terminal. |
URL Rewriting | Support for URL rewriting and redirection requests. | Before using components of the URL. |
WebSockets | Enable WebSockets protocol. | Before accepting requests assembly WebSocket required. |
Third Hand: Custom Middleware
1. Create a custom middleware class and use it to provide extension methods
2. Configure method call
3. Run
Outcome
本文介绍Asp.Net Core中间件,下局介绍路由,待续...
List
Author: home dotNET the
link: https: //www.jianshu.com/p/ff1a9bd14eb6
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.