Including middleware and why it is useful
This recipe will show you how to set up middleware in your ASP.NET Core application. Middleware in ASP.NET defines how our application responds to any HTTP requests it receives. It is also useful for controlling how our application responds to user authentication or errors. It can also perform logging operations regarding incoming request.
Getting ready
We need to modify the code contained inside the Configure()
method of our Startup
class. It is here that we set up middleware in an ASP.NET Core application. In Chapter 10, Exploring .NET Core 1.1,
we saw that our Configure()
method already contained two pieces of middleware. The first is a piece of middleware that will display a developer exception page when an unhandled exception is caught. The code looks as follows:
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
This will display any error messages which is useful for debugging the application. Typically, this page would contain information...