pcbway




Middleware in ASP.NET Core, Middleware in ASP NET Core, middleware components in asp net core, asp.net core middlewareHello friends, I hope you all are doing great. In today’s tutorial, we are going to have a look at what are Middleware in ASP.NET Core. It’s our 3rd tutorial in ASP.NET Core series. In order to understand the Middleware, we have to open the Startup.cs file present in Solution Explorer.

In our previous lecture, we have discussed the Code in Startup.cs and I have told you thhat we will discuss the Configure method later, so now we have to understand that code first. So, first let’s define middleware components and then have a look at that code:

Middleware in ASP.NET Core

  • Middleware in ASP.NET Core are simple software components used to control HTTP requests and handle both incoming requests as well as outgoing responses.
  • Each middleware is a separate object, has a specific role assigned to it and are placed one after another in the request processing pipeline, as shown in below figure:

Middleware in ASP.NET Core, Middleware in ASP NET Core, middleware components in asp net core, asp.net core middleware

  • The middleware components are used for different purposes i.e. Authentication, Logging in, Verification, File Upload ( .js, .css ) etc.
  • These middleware components operate in fix order from top to bottom, so the incoming request is first received by the 1st Middleware and after proper processing on the request, it is forwarded to the 2nd Middleware.
  • A middleware can also skip the request and without any processing, forward it to next middleware.
  • For a proper working cloud based web application, we have to add a lot of middleware components, which we will see in coming lectures.
  • There are many builtin middleware components available in ASP.NET Core as Nuget packages and we can also design custom middleware components in it.
  • So, now let’s open the Startup.cs file and have a look at the Configure Method, as shown in below figure:

Create First Web Application in ASP.NET Core, Create First Web Application in ASP NET Core, first project in asp core, first project in .net, hello world in asp.net core




  • You can see in the above code that we have a Startup Class, which has two functions in it, which we have discussed in our previous tutorial.
  • So, now let’s have a close look at the code in Configure Method, it currently has three middleware components in it.
  • Middleware in ASP.NET Core, Middleware in ASP NET Core, middleware components in asp net core, asp.net core middlewareThe first middleware components is placed inside IF loop and is shown in figure on right side.
  • You can see in the IF Loop we have Developer Exception Page, it’s a builtin ASP.NET Core Middleware Component.
  • By convention, each middleware component start with “Use” word in ASP.NET Core, so if you are designing a custom middleware component, then its better to follow this practise.
  • We will study this Developer Exception Page in detail later, but for now you just need to know that its a middle ware component which shows an exception page to the developer and as it’s inside IF Loop so it will be available only in Developers Environment, we will study Environment in coming lectures.
  • One more thing, we are using app and then dot operator to use Middleware in ASP.NET Core.
  • Second Middleware Component is app.UseRouting(), so the middleware name is Routing.
  • Finally the third middleware is End Points, which is displaying Hello World! on the page.
  • These Middlewares in the Configure Method, collectively create a pipeline structure i.e. there are executing one after another from top to bottom, this pipeline structure is called Request Processing Pipeline.

So, that was all about Middleware in ASP.NET Core. If you got into any trouble, then ask in comments and I will help you out. In the next article, we will have a look at How to add static files in ASP.NET Core. Till then take care & have fun !!!

Leave a Reply