Custom Middleware in ASP.NET Core
We can also build our own Custom Middleware in ASP.NET Core. The custom middleware can be either of 4 types: -
1. Content-Generating Middleware - The Content-Generating Middleware generates some content or Response and return Response to the client
2. Short-Circuiting Middleware - Prevents further middleware from processing the request because it short-circuits the pipeline.
3. Request-Editing Middleware - Use to edits http requests, instead of generating response
4. Response-Editing Middleware - User to Edits the response generated by other Middleware.
Each Middleware must have 2 things:
1. A RequestDelegate (Microsoft.AspNetCore.Http) object,
which is used to process the http requests. The RequestDelegate represents the next middleware component in the http pipeline and is injected to the controller of Middleware with Dependency Injection.
2. An Invoke method
which is called whenver the Middleware is called or .NET receives an HTTP request. The Invoke method has a parameter of type HttpContext which contains Information about the HTTP request and the response that will be returned to the client.
The Middleware class doesn’t implement an interface or derive from a common base class.
They have a constructor that takes a parameter of type RequestDelegate which is provided to it automatically by MVC. The RequestDelegate object represents the next middleware component in the line.
Content-Generating Middleware -
Step 1: Create Class for custom middleware
Step 2: Register the Middleware inside the Program.cs class using the UseMiddleware() method
Step 3: Check: Run the application and then go to the URL – https://localhost:44343/middleware. You will see the response from the Middleware
Inject services to Middleware with dependency injection
We can inject services to Middleware with dependency injection, by injecting that service in the constructor of the middleware.
Short-Circuiting Middleware -
Step 1: Create Class for custom middleware
The Short-Circuit Middleware checks the User-Agent in the header to find out if the browser is firefox. In that case it does not forward the request and returns unauthorized status.
Step 2: If you now run your application and open the URL – https://localhost:7164/middleware in Firefox browser. You will receive a blank page. Whereas in other browsers you will get the response from The ContentMiddleware
Request-Editing Middleware -
Step 1: Create Class for custom middleware and do some changes in RequestEditingMiddleware
Step 2: Run the application and upon the URL – https://localhost:7164/middleware in firefox. You will get the same blank page due to unauthorized HTTP request condition.
ShortCircuitMiddleware
Step 1: Create Class for custom middleware and do some changes in ShortCircuitMiddleware
if the HTTP status code in the response is 401 (as we set earlier in our Short-Circuiting Middleware), show text on browser 'Firefox browser not authorized', for other browser it show content from ContentMiddleware
Try to Browser a wrong URL https://localhost:7164/middleware1, you will get anothe message
Register Middleware Using Extention Method
We Register Middleware Using Extension method