Introduction to ASP.NET Core Middleware
ASP.NET Core brings in with an innovative concept called Middleware. They are software modules which are collected together to the application pipeline to handle the responses and requests passed. The ASP.NET Core Middleware maintains how the application responds to the HTTP requests. Middleware is the key part in authenticating and authorizing a user to achieve exact actions. It also manages the application if there any error has occurred. The each and every part of Middleware in ASP.NET Core is an object, and every part has a particular purposeful role.
What is ASP.NET Core Middleware?
- ASP.NET Core Middleware is a software component which is composed to the application pipeline to handle the requests and responses. Middleware is a component or class which is implemented on every request in the ASP.NET Core application. Therefore, each and every component decided to select whether to pass the request to the subsequent component in the pipeline.
- Consider we are having the middleware component to handle the errors. Another middleware authenticates a user and another middleware to serve the static files like CSS Files, Images, JavaScript files, etc.
ASP.NET Core Request Processing
In ASP.NET, there is request pipelines called HttpHandlers and HttpModules, which are similar to the middleware both require be configuring and implementing in each and every request. ASP.NET Core Middleware maintains how the application responds to the HTTP requests. Middleware is the key part in authenticating and authorizing a user to achieve specific actions.
In general, there is numerous Middleware in ASP.NET Core Web-Application. Those middleware are in two types it may be own custom middleware, or it’s a framework middleware added through NuGet. In the requested pipeline, we can position the middleware in order-wise. Each middleware adds and alters the HTTP Request and preferably passes the controls to the subsequent middleware component.
The Middleware built the requesting pipeline, which describes the ASP.NET CORE Request Processing.
How to Add another ASP.NET Core Middleware?
ASP.NET Core Middleware maintains how the application responds to the HTTP requests. Thus, middleware is the key part in authenticating and authorizing a user to achieve specific actions.
Let’s see the following steps to include the additional middleware.
- For adding the Middleware, we need to right-click the project and choose the Manage NuGet Packages.
- Then look for Microsoft.AspNet.Diagnostics that’s the real ASP.NET Core Middleware for exception display pages, diagnostics information, and exception handling. This selected package includes various parts of Middleware we can make use of.
- Install that package if it is not installed in your project.
- To install the package called Microsoft.AspNet.Diagnostics, if it’s not already installed in the project.
- And then, go to the Configure() method and invoke the app.UseWelcomePage Middleware.
// the Configure () method will be called during runtime
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseWelcomePage();
// by using the method to configure the HTTP Request pipeline
app.Run(async (context) =>
{
var msg = Configuration["Message"];
await context.Response.WriteAsync(msg);
});
}
}
- Finally, execute the application and see the screen as follows.
The above screen is just the welcome page; let’s use the RuntimeInfoPage by giving the following code.
// the Configure () method will be called during runtime
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseRuntimeInfoPage(); //here the UseRuntimeInfoPage() method
app.Run(async (context) =>
{
var msg = Configuration["message"];
await context.Response.WriteAsync(msg);
});
} // by using the method to configure the HTTP Request pipeline
- Update the Startup.cscode page, save it, refresh the browser, and see the following screen.
The RuntimeInfoPage is the Middleware which responses to the specific URL requests. If the incoming request does not match the URL, then the middleware part just passes the request to the next portion of Middleware. And then, the request passes through the IISPlatformHandler Middleware; after that, it goes to the UseRuntimeInfoPage() Middleware. Finally, it will go to the app.Run and just displays the string.
- Finally, add the “/runtimeinfo” at the end of the URL and now look at the page which generated by that RuntimeInfoPage Middleware.
And then look at the response, which gives few details on the Runtime Environment like Operating System, Runtime Versions, their type, Architecture and the entire packages which you were making use of it and so on.
Run Method
The Run Extension method is to includes the Middleware; let’s see the following format method used for Run Method ().
public static void Run(this IApplicationBuilder app, RequestDelegate handler)
In this Run Method () is an extension method on the IApplicationBuilder and also accepts the attribute of RequestDelegate. This RequestDelegate is a type of delegate method that handles the request.
Let’s see the following RequestDelegate format as follows.
public delegate Task RequestDelegate(HttpContext context);
The Run method() accepts the method as an attribute, and their syntax match with the RequestDelegate. So the method agrees with the HTTPContext attribute and returns the task. So we can make use of either a particular Lambda Expression or a particular Function in Run Method().
Let’s make the asynchronous by using the await and async to enhance the performance and scalability.
app.Run(async context => await context.Response.WriteAsync("Hello World!"));
//or else make use of
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
Example of ASP.NET Core Middleware
Middleware is a component or class which is implemented on every request in the ASP.NET Core application. ASP.NET Core Middleware is a software component which is composed to the application pipeline to handle the requests and responses. Each and every component decided to select whether to pass the request to the subsequent component in the pipeline.
Let’s see the following diagram, which describes the implementation of middleware components.
Conclusion
This article shows how the ASP.NET Core Middleware components handle the request processing pipeline and how it controls our application HTTP request responses. Each and every middleware component in ASP.NET Core has accessing the incoming requests and outgoing responses.
Recommended Articles
This is a guide to ASP.NET Core Middleware. Here we discuss the introduction, how to add another ASP.NET core middleware? run method and example. You may also have a look at the following articles to learn more –