concept custom middleware component in category asp.net

appears as: custom middleware components, custom middleware component
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

  • Filters can be easily applied to a subset of requests; for example, all actions on a single controller. Middleware doesn’t have this concept as a first-class idea (though you could achieve something similar with custom middleware components).
  • In this section, I show three ways to create custom middleware components, as well as how to create branches in your middleware pipeline where a request can flow down either one branch or another. By combining the methods demonstrated in this section, you’ll be able to create custom solutions to handle your specific requirements.

    Custom middleware components don’t derive from a particular base class, but they have a certain shape, as shown in listing 19.5. In particular, middleware classes should have a constructor that takes a RequestDelegate object, which represents the rest of the middleware pipeline, and they should have an Invoke function with a signature similar to

    Listing 19.5. Adding headers to a Response using a custom middleware component
    public class HeadersMiddleware { private readonly RequestDelegate _next; #1 public HeadersMiddleware(RequestDelegate next) #1 { #1 _next = next; #1 } #1 public async Task Invoke(HttpContext context) #2 { context.Response.OnStarting(() => #3 { #3 context.Response.Headers["Strict-Transport-Security"] = #3 "max-age=60"; #3 return Task.CompletedTask; #3 }); #3 await _next(context); #4 } } #1 The RequestDelegate represents the rest of the middleware pipeline #2 The Invoke method is called with HttpContext when a request is received. #3 Adds the HSTS header response as before #4 Invokes the rest of the middleware pipeline. Note that you must pass in the provided HttpContext. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":70},{\"line\":2,\"ch\":72}],[{\"line\":3,\"ch\":70},{\"line\":3,\"ch\":72}],[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":5,\"ch\":70},{\"line\":5,\"ch\":72}],[{\"line\":6,\"ch\":70},{\"line\":6,\"ch\":72}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":2,\"ch\":70},{\"line\":2,\"ch\":72}],[{\"line\":3,\"ch\":70},{\"line\":3,\"ch\":72}],[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":5,\"ch\":70},{\"line\":5,\"ch\":72}],[{\"line\":6,\"ch\":70},{\"line\":6,\"ch\":72}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":2,\"ch\":70},{\"line\":2,\"ch\":72}],[{\"line\":3,\"ch\":70},{\"line\":3,\"ch\":72}],[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":5,\"ch\":70},{\"line\":5,\"ch\":72}],[{\"line\":6,\"ch\":70},{\"line\":6,\"ch\":72}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":2,\"ch\":70},{\"line\":2,\"ch\":72}],[{\"line\":3,\"ch\":70},{\"line\":3,\"ch\":72}],[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":5,\"ch\":70},{\"line\":5,\"ch\":72}],[{\"line\":6,\"ch\":70},{\"line\":6,\"ch\":72}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":2,\"ch\":70},{\"line\":2,\"ch\":72}],[{\"line\":3,\"ch\":70},{\"line\":3,\"ch\":72}],[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":5,\"ch\":70},{\"line\":5,\"ch\":72}],[{\"line\":6,\"ch\":70},{\"line\":6,\"ch\":72}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":8,\"ch\":70},{\"line\":8,\"ch\":72}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":10,\"ch\":70},{\"line\":10,\"ch\":72}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":12,\"ch\":70},{\"line\":12,\"ch\":72}],[{\"line\":13,\"ch\":70},{\"line\":13,\"ch\":72}],[{\"line\":14,\"ch\":70},{\"line\":14,\"ch\":72}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":17,\"ch\":70},{\"line\":17,\"ch\":72}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}]]"} !@%STYLE%@!