Understanding built-in middleware

So, what are the use cases for middleware? As discussed earlier, they are usually related to cross-cutting concerns such as logging, authentication, and exception handling. ASP.NET Core itself provides some built-in middleware that represents a standard way to solve problems:

  • UseStaticFiles(): Provides a way to deal with static files and assets inside your application. When the client asks for a static resource, this middleware filters the request and returns the requested file without hitting the rest of the pipeline.
  • AddResponseCaching(): Helps developers to configure the caching system of the application. This middleware also adds all HTTP-compliant information related to the cache.
  • UseHttpsRedirection(): This new, built-in piece of ASP.NET Core 2.1  middleware provides a way to force HTTPS redirection.
  • UseDeveloperExceptionPage(): This shows a detailed error page (the new YSOD) in the case of exceptions. This is usually conditionally initialized, depending on the environment.

These are some built-in pieces of middleware provided by ASP.NET Core. As you can see, all middleware provides cross-cutting functionalities for your application. What's important here is that the order of middleware initialization reflects the order of our pipeline; for example:

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseHttpsRedirection();
app.UseStaticFiles();
}

In this case, the UseStaticFiles middleware will never receive requests for static files because the MVC middleware handles them first. A general rule is to place UseHttpsRedirection() as the last middleware in the pipeline; otherwise, other middleware will not intercept requests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.16.254