ASP.NET Core Use(), Next(), Map() and Run() Middleware Extension Methods

ASP.NET Core is introduced a concept called Middleware. When you make a request to server, a series of middleware components will be executed in HTTP Pipeline. In this article we will look into both Middleware and HTTP Pipeline under the below topics:
 
    • What is Middleware?
    • How to configure or use Middleware?
    • Use() method
    • Next() method
    • Map() method
    • Run() method

ASP.NET Core Middleware


What is Middleware?

A Middleware is nothing but a piece of logic or code, that can be injected in to HTTP Request-Response Pipeline to handle your requests. Generally this middleware is represented or exposed as extension method, which are going to discuss in below sections.

Before we understand much better about ASP.NET Core Middleware, let’s see how traditional MVC application process the requests as shown below:

ASP.NET MVC HTTP Request Pipeline

When you look at the above diagram, the execution flow of the request and response in traditional MVC is as below:
  • When you browse for a URL, the request goes to HTTP Request Pipeline
  • The single HTTP pipeline will be executed
  • And then the request goes to your MVC Application
  • MVC Application process your request and response passed back to HTTP pipeline
  • The HTTP pipeline send the response back to the client browser.

In the same above pattern now we will see how the request and response are processed by ASP.NET Core Application. The below diagram illustrates how the requests are served in ASP.NET Core:

ASP.NET Core HTTP Request Pipeline

The above diagram explains that how the request and response flows in ASP.NET Core HTTP Pipeline. As you can see there were multiple middlewares in HTTP Pipeline and each pipeline will be called one after another. The important thing in HTTP Pipeline is that placing the middleware in the order the way you need, since the HTTP Pipeline calls them in the same order and when the response sending back, pipeline calls them in the same reverse order.

How to configure or use Middleware?

In ASP.NET Core, Middleware will be configured using extension methods called Run(), Use() and Map().

All these above methods are extension methods to IApplicationBuilder interface and accepts RequestDelegate type (a function) which handles your request. The ASP.NET Core works as Async mode, so the RequestDelegate should be an Async method.

Please note that the order of the middleware configuration is important as stated in above section.

Use() and Next() methods

The Use() extension method in ASP.NET Core is used to add/insert new middleware component to HTTP Request Pipeline. And, the Next() extension method is used to call the next middleware component in HTTP Request Pipeline.

[pre class="brush:csharp; toolbar: false;" title=""] app.Use(async (context, next) => { await context.Response.WriteAsync("This text is from Use1 method.\n"); await next(); //calls below middleware (Use2 method) //the logic here called when the response sending back to user, (and not while making request) }); app.Use(async (context, next) => { await context.Response.WriteAsync("This text is from Use2 method.\n"); await next(); //the logic here called when the response sending back to user, (and not while making request) }); [/pre]
In the above code you can see that await next() calls another next occurrence middleware in pipeline. When you run above code, it outputs as:

ASP.NET Core Use() method
Run() method

The Run() extension method in ASP.NET Core is used to end the execution of the pipeline, it means that Run() extension method is the last middleware and which will not call the next middleware in HTTP Request Pipeline. Any other middleware added after the Run() method will not be called and ignored.

[pre class="brush:csharp; toolbar: false;" title=""] app.Run(async context => { await context.Response.WriteAsync("This text is from Run1 method.\n"); }); //The below middleware will never be executed app.Run(async context => { await context.Response.WriteAsync("This text is from Run2 method.\n"); }); [/pre]

ASP.NET Core Run() method
Map() method

The Map() extension method in ASP.NET Core is used to map the middleware to a specific URL.

[pre class="brush:csharp; toolbar: false;" title=""] app.Map("/callme", CallMeURL); static void CallMeURL(IApplicationBuilder app) { app.Run(async context => { await context.Response.WriteAsync("This text is from Map path URL (Map method)"); }); } [/pre]
As you can see in the above code that, the first parameter accepts the path of the URL and the second parameter with a function to perform some logic upon calling first parameter URL.

ASP.NET Core Map() method
From the above example, when you browse with /callme then ASP.NET Core match the URL with all configured Map() method URLs and called respective function when found.

That’s all brief about ASP.NET Core Use(), Next(), Map() and Run() middleware extension methods. Hope you have enjoyed with this article, and if you have any queries, please use the below comment box.

No comments:

Powered by Blogger.