What Are Middlewares in .NET

Yavuz Selim GÜLER
2 min readMar 23, 2022

Many new concepts introduced with .NET Core and middleware is just one of them.

Middleware is software that handles requests and responses in a pipeline. Processes the request to make a response and manages them to pass the next pipe on the line. In the older versions of ASP.NET HttpHandlers and HttpModules were the components of the request pipeline.

Request Delegates are used to handle the HTTP requests and build the request pipeline. Request Delegates configured using these middleware extension methods;

app.Run()

This method acts like terminal middleware. Run() method should be used at the end of the pipeline. Because we can not include next parameter with Run() method like Use() method.

app.Use()

For configuring multiple middleware we can use Use() method. Unlike Run() method next parameter can be included with Use() method. Next parameter calls the next request delegate in the pipeline. If we don’t give next parameter there is no difference between Use() and Run().

app.Map()

This extension method are used for creating new branches on the pipeline. Map branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed.

Summary

Briefly, middleware handles responses to HTTP requests. Every middleware;

  • Has access to the request and the response.
  • Can pass the request to the next middleware in the pipeline.
  • Can perform some logic and then pass that request to the next middleware for further processing.
  • Can terminate(kill) the request pipeline when required.
  • Is executed in an order which they are added to the pipeline.

I hope you all enjoyed reading. Hope to see you soon!

References: TutorialsTeacher, Microsoft Docs, C# Corner

--

--