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

By: Mayur
Updated: March 3rd, 2017 | Technology: Asp.Net

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 is a software component which is assembled into an application to handle request and response to performing some actions before or after the 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 an error page using “UseExceptionHandler()”. And if we don’t get any type of errors than middleware directly call the next component.

How to Use Asp.Net Core Middleware?

Copy to Clipboard

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

In the 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 every single response.

How to Add Middleware in Our Application?

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

  1. Right click on the project and select Manage NuGet packages.
    middleware
  2. In Manage Nuget Package console search for Microsoft.aspnetCore.diagnostics and Click on Install will Install library of Exception Handling In our project.
    middleware
  3. Now click on Startup.cs file which is on the root of our application.

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

    if (env.IsDevelopment()){
    app.UseDeveloperExceptionPage();
    }

    middleware

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.