In this tutorial, I will show you the concept of ASP.NET Core Middleware like What is Middleware? How to configure middleware in ASP NET Core 2.0 Application, Built-In middleware plugins, and how to add middleware from NuGet Package.
In the previous tutorials of this ASP NET Core 2.0 series, I have discussed the following topics
- Introduction to ASP NET Core 2.0
- How to create a project in ASP NET Core 2.0 using Visual Studio 2017.
- Project folder structure and responsibility of each file in ASP.NET Core 2.0 Application.
And now in this ASP.NET Core tutorial, below are some points that you will learn in this tutorial
- What is middleware in ASP.NET Core 2.0?
- How to Configure middleware in ASP NET Core 2.0?
- Built-In middleware plugins in ASP NET Core.
- How to add Middleware plugin from NuGet Package.
What is middleware in ASP.NET Core?
Basically, ASP.NET Core introduced a new concept called middleware. Middleware is nothing but software component that is assembled into an application pipeline to handle requests and responses. In ASP.NET Core, middleware controls how application respond to HTTP requests, how to authenticate and authorize a user to perform specific actions, how the application behaves if there is an error.
There will be multiple middlewares in ASP.NET Core Web Application. It could be a framework provided middleware or your own custom middleware. Each component of middleware chooses whether to pass the request on the next middleware or component to the next one in the pipeline and can perform some specific actions before and after the next middleware invoked in the pipeline. The following figure illustrates the behavior of middleware in ASP.NET Core.
In middleware there are different pieces and each piece of middleware in ASP.NET Core is an object that has very specific and limited role. So, we need many pieces of middleware for an application to produce result properly.
How to configure middleware in ASP.NET Core?
Let’s move ahead and take a simple example to get more understanding of middleware in ASP.NET Core.
We can configure middleware in the Configure method of our startup class using IApplicaitonBuilder Instance.
The above example adds a middleware using UseStaticFiles() method which Provides support for serving static files and directory browsing. And after that, one more middleware UseMvc() is added in our ASP.NET Core application.
How to add Built-in Middleware in ASP NET Core App.
There are many other built-in middleware plug-ins available at NuGet Package. That we can use in our ASP.NET Core application. See below the list of Built-in plugins.
- Authentication: Provides authentication support.
- CORS: Configures Cross-Origin Resource Sharing.
- Diagnostics: Configures diagnostics.
- Response Cashing: Provides support for caching responses.
- Response Compression: Provide support for compressing support.
- Routing: Defines and constrains request routes.
- Session: Provides support for managing the user session.
- Static Files: Provides support for serving static files and directory browsing.
- URL Rewriting: Provides support for rewriting URLs and redirecting requests.
- WebSockets: Enables the WebSockets protocol.
Let’s take a look how to add another middleware in ASP.NET Core application using NuGet package. We will add “Microsoft.AspNetCore.Diagnostics” in our project. Diagnostics is actually middleware for exception handling, exception display pages, and diagnostics information. This package contains many different pieces of middleware that we can use.
Let’s start step by step.
- Just go to solution explorer => right click on project => choose manage NuGet Packages.
- Search for “Microsoft.AspNetCore.Diagnostics” and then install it.
- Now, just invoke app.UseWelcomePage middleware in configure() method just below the UseMvc() service.
- Now run your project and then you will see the following output.
- Now Change the URL with this one “http://localhost:65388/admin/Index”, Then you will see the below output because we don’t have any specific admin controller and its view. This is the by default page from UseWelcomePage() middleware.
Leave a Reply