Middleware Components (.Net Core) -Part 1
By: Avinash Karat
Tags: C Sharp Programming, Dotnet, Middleware, Dotnet Core

Coming from a background of ASP.Net Webforms and ASP.Net MVC, one of the main features which strike me in .Net Core is the Middleware Components. If you have worked with earlier versions of .Net, prior to Core, you might have heard about the http handlers and modulers(I had a hard time learning it though). Well, in .Net Core, they are sort of replaced and in its place a single entity emerged and it is called the “Middleware Components”. So they can be viewed as something we can use to intercept the request, modify it while it goes through the system, and also can be used to alter the response. Simply speaking, middleware components, arranged in a chain, forms the request pipeline.

Middleware components are written (or registered) in the “Configure” method of “StartUp” class.

There are mainly two types of middleware components. Standard(built in) and custom middleware components.

let’s look at some of the standard ones(they are easy to understand also).

Welcome Page Middleware

We can create a simple welcome page for our .Net core app, using the “WelcomePageMiddleware”. This can be handy when you first set up the application, and ensure the requests are getting processed without any issues.

In the ‘Configure’ method of the startup class, replaces all the default code and just put app.UseWelcomePage();

(As I mentioned in my previous blog post, .Net core apps are highly modular. If we just want a simple welcome page, then we don’t need to start the application with all the burden of MVC or Web API components. We can add those things only when needed, along the way)

If we run the application now, we can see a simple, but beautiful welcome page

2. Static Files Middleware

This middleware components are used in an application which serves just static files from wwwroot folder when requested. For example, I have created a static html file called somemessage.html, and put it inside the wwwroots folder. Also I have updated the configure method as shown below:

when we hit F5, we can see the result as follows:

3. StatusCodePagesMiddleware

We generally see 500 Internal server error when some catastrophic error occurs at the server side which is not handled, or a 404 Not found error when the user sends an invalid request. Without properly handling these kinds of errors, the end user will be confused and may think that the application is broken.

In these kinds of scenarios, we can use the statuscodepagesmiddleware to display a friendly error code, as shown below.

4. DeveloperException Page Middleware

When you are in the development phase of your application, you wish to get as much information as possible when an error occurs. Well this middleware exactly gives you that. For demo purpose, I have set up a sample MVC application, and deliberately introduced a Null Reference exception.

here I will show you with/without the DeveloperExceptionPage middleware.

a) Without DeveloperExceptionPage

And the output is:

here, we just get a 500 Internal error. There are no specifics. Now lets look at the other case

b) With DeveloperExceptionPage

here I un-commented the UseDeveloperExceptionPage() method.

and here is the output:

I think the picture itself is self explanatory. Here we get the complete details o the exception including the stack trace, in a visually appealing manner.

5. ExceptionHandlerMiddleware in production.

The DeveloperExceptionMiddleware is handy when you are in the development phase of the application. But what about once the app is pushed to the production? Revealing the detailed information about an error can be useful for the malicious attackers. To get around this problem we can use ExceptionHandlerMiddleware. If an error occurs in production, the user is redirected to a pre-defined error page(without revealing the stack trace and all), which is consistent in look and feel with the rest of the application. For this, we need to incorporate minor changes only in the Configure method.

Conclusion:

Well, that was a humble beginning to middleware components. Lets look at how can we create custom middleware components in the next post.

Thanks for reading!

Say Hi to Author:

LinkedIn