Companies: | 114,335 |
Products and Services: | 5,224 (+1) |
Articles and publications: | 2,513 |
Tenders & Vacancies: | 595 |
Want an absolute control over the request/response action of your application?
Middleware- the catalyst behind the soaring popularity of ASP.NET Core, can help you with this, allowing you to control each request and response activity of your application and making the software architecture comprehensible and meaningful.
Middleware, also termed as a software glue, is computer software that allows the applications/software to interact with the database, servers, remote systems, etc, depending on the type used. Middleware makes the communication within the application easier, controlling the HTTP requests and responses.
Before its introduction, the request and response objects in the ASP.NET projects, used to be very big as well as extremely tightly coupled with the web server software i.e. Internet Information Services(IIS), making the objects difficult to handle. To solve this ASP.NET Core Middleware came into the picture, which showed the way to remove the dependency of the web applications on the server.
To use the middleware in the application, you need to build the request pipeline using the request delegates, which handles the HTTP request. You can configure the request delegates through the ‘Run’, ‘Map’, and ‘Use’ method on IApplicationBuilder.
These delegates decide whether the request should be passed to the next component in the application pipeline or not. Along with this, it can also perform the required actions such as saving data, before and after the invocation of the next component in the application pipeline. Then the response comes back in the same fashion through the pipeline.
The Run method is an extension method that handles the requests, accepting the RequestDelegate parameter. The Run() method should be placed at the end of the pipeline as the method terminates the chain, preventing any other middleware to execute.
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from " + _environment);
});
The Use() method enables you to perform the action before and after the next middleware delegate. You can also call the required middleware delegate using the function next.Invoke(). It helps include the short-circuiting in the pipeline – a process in which the middleware, instead of passing a task to another middleware, executes it.
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello from " + _environment);
...
await next.Invoke(); // invoking the next middleware
});
The Map() method checks the path requested by the user and matches it with the path present in the parameter, finding the match the method runs the middleware. It also creates multiple paths and hence the terminating ends for the pipeline.
You can use multiple Middleware in the ASP.NET Core based web application as per the requirement, some are inbuilt within the frameworks and some of them you need to add through NuGet.
ASP. NET provides following built-in middleware components:
Middleware | Description |
Authentication |
Provides support for authentication in website and application |
Session |
Provides support for user sessions management. |
Routing |
Provides support for requested routes. |
CORS |
Provides Cross-Origin Resource Sharing configuration. |
Static Files |
Provides support for fetching and retrieving static files |