ASP.NET Core Use(), Next(), Map() and Run() Middleware Extension Methods
- What is Middleware?
- How to configure or use Middleware?
- Use() method
- Next() method
- Map() method
- Run() method
What is Middleware?
A
Before we understand much better about ASP.NET Core Middleware, let’s see how traditional MVC application process the requests as shown below:
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.
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
[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
Run() method
The
[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]
Map() method
TheMap() 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.
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.
Map() method
The
[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.
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: