Skip to main content

ASP.NET Core – Middleware

 ASP.NET Core – Middleware

 

A middleware is nothing but a component (class) which is executed on every request in ASP.NET Core application.

There will be multiple middleware in ASP.NET Core web application.

1.    Configure Single middleware.

2.    Configure Multiple middleware.

3.    Configure Own custom middleware.

4.    We can set the order of middleware execution in the request pipeline.

 

 



The following figure illustrates the ASP.NET Core request pipeline processing.

 


Configure Middleware

 

We can configure middleware in the Configure method of the Startup class using IApplicationBuilder instance

 

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");  // Middlewear – (1) in Pipeline
}

 

app.Run(async (context) =>

{             

   await context.Response.WriteAsync("Hello World!"); //Middlewear – (2) in Pipeline

             

});

 

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Worker Process for hosting -> " +
System.Diagnostics.Process.GetCurrentProcess().ProcessName); //
Middlewear – (3) in Pipeline
});
});

}

 

In the above example, Run() is an extension method on IApplicationBuilder instance which adds a terminal middleware to the application's request pipeline.

 

Understand Run Method

 

We used Run extension method to add middleware. The following is the signature of the Run method:

 

public static void Run(this IApplicationBuilder app, RequestDelegate handler)

 

The Run method is an extension method on IApplicationBuilder and accepts a parameter of RequestDelegate.

 

Understand RequestDelegate Method

The RequestDelegate is a delegate method which handles the request to receive the httpcontext request

 

public delegate Task RequestDelegate(HttpContext context);

Comments

Popular posts from this blog

How to perform lazy loading with generic method.

Introduction Let’s develop C# console application to perform lazy loading with generic method.    Getting Started In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project. Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button. We need to include the following references in our application. using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 classes and 1 method EducationProfile<T> – To define class members. Candiate<T> = Initialize lazy loading in the class. CallLazyLoading = call the function Please refer to the code snippet below static   void  Main( string [] args)   {        try ...

How to perform Insert/Update/Delete operation in Generic Collections in C#

Getting Started In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project. Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button. we need to include the following references in our application. using System; using System.Collections.Generic; Insert, Update, Delete, Get perform in List collection. public static void DML_List() { // Creating a List of integers IList<int> list = new List<int>(); // Printing the insert opeartion Console.WriteLine("INSERT"); // Adding elements in list collection list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(400); // Printing the element from list collection foreach (object x in list) { Console.WriteLine("Result : " + x); } Console.WriteLine("\n"); // Printing th...