HttpContext in ASP.NET Core

In the previous example, we saw how to create middleware using the app.Run extension method. A key concept involved in that implementation is the HttpContext type, which is the unique entry point for obtaining all information about HTTP properties; it is usually related to the incoming request. The HttpContext attribute exposes methods and properties to get information from the request and update information in the response. The response and request information are represented by the following attributes: HttpContext.Response and HttpContext.Request. For example, in the previous case, we used the  WriteAsync method, which wrote the Hello World! string in response to the current HttpContext.

Dependency injection is a core part of ASP.NET Core. HttpContext has all references to services instantiated in the current request. To be specific, it provides a RequestServices property, which refers to the service container. We will explore dependency injection in more detail in the next chapter. Declaring a piece of inline middleware using the app.Run method is not the only way to define new middleware. Furthermore, in the following sub-section, we will see how to build middleware logic using a class-based approach.

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

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