What is Middleware in ASP. NET Core And How Does It Work?

29sep 2020
What is Middleware in ASP. NET Core And How Does It Work?
Middleware is one of the most important layers in ASP. NET Core, taking software development to next level. In this article, know more about . NET Middleware and How does it work.

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.

So, what exactly is the Middleware?

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.

How does Middleware Works?

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.

  • Run method

    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);
    });
  • Use method

    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
    });
  • Map method

    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.

    How does Middleware Works

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

How to set up a Middleware Pipeline in your Website?

Check it out right here at ASP.NET Core Middleware – A Layered System.

Related items:

ASP. NET Core: What Is It and Top 5 Advantages of . NET Core 9/29/2020 ASP. NET Core: What Is It and Top 5 Advantages of . NET Core
We wIll learn in this article about What is ASP. NET Core and the Advantages of using ASP. NET Core? Here are the Top 5 Advantages of . NET Core.
Authentication for Single Page Apps (SPA) on ASP. NET Core 9/24/2020 Authentication for Single Page Apps (SPA) on ASP. NET Core
ASP. NET Core offers authentication in Single Page Apps (SPAs) using the support for API authorization. Here is the guide on it.
What is Shopify? How Does it Work for Small Business in 2020 9/24/2020 What is Shopify? How Does it Work for Small Business in 2020
This article explains, how Shopify will work for Small Businesses, Sellers, and Merchants in this pandemic.
How the Hell Exactly Would You Choose Web Technologies? 7/15/2022 How the Hell Exactly Would You Choose Web Technologies?
Web development is fairly easy to learn, easy to do small projects but will go crazy in the large-scale.
Facebook Shops: The eCommerce Game Changer in 2020 9/24/2020 Facebook Shops: The eCommerce Game Changer in 2020
Facebook announces partnership with eCommerce platforms. Learn more about What is Facebook shops and its integrations with eCommerce.
×
Ace Infoway: Website and Mobile Applications Development