What is Asp.Net Core Middleware? How to Use Middleware in Web Application?

 Introduction to Asp.Net Core Middleware:

In Simple term, if we say what is Middleware than it’s nothing but a bridge between Database and application. Let’s say, in Asp.Net Core Middleware are also bridge provider between two components. Middleware determines How to responds to HTTP Request in Asp.Net Core.

So let’s say Middleware are software component which is assembled into an application to handle request and response to performing some actions before or after next component Invoke.

Suppose in our application we need to handle an error in a different way, when we are in development mode then we handle error using “UseDeveloperExceptionPage()” otherwise we will redirect to error page using “UseExceptionHandler()”. And if we don’t get any type of errors than middleware directly call next component.

How to Use Asp.Net Core Middleware ?

using Microsoft.AspNet.Builder;

using Microsoft.AspNet.Hosting;

using Microsoft.AspNet.Http;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Configuration;

namespace MyFirstCoreApplication


public class Startup


public Startup()


var builder = new ConfigurationBuilder()


Configuration = builder.Build();


public IConfiguration Configuration { get; set; }

// This method gets called by the runtime.

// Use this method to add services to the container.

// For more information on how to configure your application,

// visit http://go.microsoft.com/fwlink/?LinkID=398940

public void ConfigureServices(IServiceCollection services)



// This method gets called by the runtime.

// Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app)



app.Run(async (context) => {

var msg = Configuration[“message”];

await context.Response.WriteAsync(msg);



// Entry point for the application.

public static void Main(string[] args) => WebApplication.Run(args);



Let’s say in above code there is one Configure Method, we will add our middleware inside this method using IApplicationBuilder interface.

In above code, you saw that Inside Configure method there is two middleware are already present in our empty project.

  1. app.UseIISPlatformHandler();

  2. app.run();

  • IISPlatformHandler: Basically IISPlatformHandler allow to work with windows identity, and It will check every incoming request and see whether any window identity information is associated with every request or not and based on that information it will call next middleware.
  • App.run: Basically app.run middleware used to register middleware and run method will pass to another method, and we can use to process each single response.

How to Add Middleware in Our Application?

Now, we will see how to add middleware in our application using NuGet Packages.

Step-1: Right click on project and select Manage NuGet packages



Step-2:  In Manage Nuget Package console search for Microsoft.aspnetCore.diagnostics and Click on Install will Install library of Exception Handling In our project



Step-3: Now click on Startup.cs file which is on root of our application

And find Configure method and Write this piece of code inside Configure method to add asp.net core middleware in our application

if (env.IsDevelopment()){




Keep being with us for more tips and tricks about the ASP.NET Core Development and Middleware concepts.

I hope you would like my blog help for the awareness about middleware and how to use middleware concept in the real-time websites and web apps. You can share this blog via a social button for helping other Asp.Net Developers.

By | 2017-07-03T16:04:15+00:00 March 3rd, 2017|Asp.Net, How To|