Fundamentals On Middleware
Fundamentals On Middleware
Fundamentals On Middleware
Typically, there will be multiple middleware in ASP.NET Core web application. It can be either
framework provided middleware, added via NuGet or your own custom middleware. We can set
the order of middleware execution in the request pipeline. Each middleware adds or modifies
http request and optionally passes control to the next middleware component. The following
figure illustrates the execution of middleware components.
AS
P.NET Core Middleware
Middlewares build the request pipeline. The following figure illustrates the ASP.NET Core
request processing.
Configure Middleware
We can configure middleware in the Configure method of the Startup class using
IApplicationBuilder instance. The following example adds a single middleware using Run
method which returns a string "Hello World!" on each request.
});
Method Signature:
public static void Run(this IApplicationBuilder app, RequestDelegate handler)
Method Signature:
public delegate Task RequestDelegate(HttpContext context);
As you can see above, the Run method accepts a method as a parameter whose signature should
match with RequestDelegate. Therefore, the method should accept the HttpContext parameter
and return Task. So, you can either specify a lambda expression or specify a function in the Run
method. The lambda expression specified in the Run method above is similar to the one in the
example shown below.
The above MyMiddleware function is not asynchronous and so will block the thread till the time
it completes the execution. So, make it asynchronous by using async and await to improve
performance and scalability.
//or
To configure multiple middleware, use Use() extension method. It is similar to Run() method
except that it includes next parameter to invoke next middleware in the sequence. Consider the
following example.
Example: Use()
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello World From 1st Middleware!");
await next();
});
The above example will display Hello World From 1st Middleware!Hello World From
2nd Middleware! in the browser.
Thus, we can use Use() method to configure multiple middlewares in the order we like.
Middleware Description
Authentication Adds authentication support.
CORS Configures Cross-Origin Resource Sharing.
Routing Adds routing capabilities for MVC or web form
Session Adds support for user session.
StaticFiles Adds support for serving static files and directory browsing.
Diagnostics Adds support for reporting and handling exceptions and errors.
Let's see how to use Diagnostics middleware.
Diagnostics Middleware
Let's install and use Diagnostics middleware. Diagnostics middleware is used for reporting and
handling exceptions and errors in ASP.NET Core, and diagnosing Entity Framework Core
migrations errors.
This package includes following middleware and extension methods for it.
We can call respective Use* extension methods to use the above middleware in the configure
method of Startup class.
Let's add welcomePage middleware which will display welcome page for the root path.
The above example will display the following welcome page for each request.
This way we can use different Use* extension methods to include different middleware.
Next, learn how to implement logging functionality in the ASP.NET Core application.