Objective
This article introduces ASP. Middleware to handle HTTP packets, leaving a record for yourself and hoping to help developers in need.
Structure
In ASP., each HTTP request packet from the "browser" is encapsulated by the system as a "httprequest object, and the default HttpResponse object, Session object, Claimsprincipal Object ... Wait for the object. These objects are then encapsulated as a "httpcontext object" to provide for the subsequent use of ASP.
After receiving HttpContext, ASP. NET core will give it to a "pipeline" to handle. This pipeline is configured with many "middleware". The system will pass the HttpContext, in sequence, to pipeline in the middleware to deal with. Each middleware processes the HttpContext in accordance with its own internal program logic, and changes the contents of the objects encapsulated by HttpContext.
After receiving a HttpContext that has been processed by middleware, the ASP. NET core takes out the HttpResponse object it encapsulates. Then according to this HttpResponse object, to establish the "Server callback" HTTP response packet content.
The ASP. NET core through the above system structure, complete the HTTP request packet input, HTTP response packet output workflow.
Development
in [ASP. NET core] Getting started this article provides an example of an ASP. NET Core middleware: Helloworldmiddleware. In this example, middleware provides the program logic that it encapsulates by actually making the Invoke method.
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; }}
When you do the Middleware.invoke method, the developer can get the HTTP Request packet content from "browser incoming" through HttpContext.Request. In the following example program code, it is through the httpcontext.request path, querystring two properties, respectively, to obtain the HTTP Request packet URL path and 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; }}
Also in the real Middleware.invoke method, developers can use Httpcontext.response to set the HTTP Response packet content from "Server callbacks". In the following example program code, it is through the Httpcontext.response WriteAsync method, StatusCode properties, respectively, to set the HTTP response packet content and 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; }}
And in the actual Middleware.invoke method, if the program code occurred in the unexpected exception. The ASP. NET core preset uses the "500 Internal Server error", which statuscode to notify the system of an exception. In the following example code, an exception error is thrown directly, which is handled by the error handling mechanism of ASP.
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; }}
When establishing the middleware, developers can refer to the next middleware in pipeline by constructing the requestdelegate that the child has passed in. By calling Requestdelegate, you can invoke the next middleware invoke method in pipeline. In the following example program code, it is called Requestdelegate to invoke the next middleware invoke method in pipeline, in order to string the other middleware program logic.
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 // ... }}
Reference
[ASP. NET Core] Middleware