Views Predefined Properties
In this lesson, we will learn the predefined properties that expose the views data computed throughout the whole ASP.NET Core pipeline and in the controllers.
We'll cover the following...
Introduction
Views expose data computed by the ASP.NET Core pipeline modules and by the controller that invoked the view. Data computed by the pipeline is exposed through the Context
properties, while data computed in the controller is exposed through the ViewContext
property.
Some nested properties of ViewContext
that are more frequently used are also exposed as first-level properties for simplifying their usage. Views also offer a few other properties and utility methods.
Context, Request, and Response
The Context
property contains an HttpCpntext
instance that stores all data produced by all modules of the ASP.NET Core pipeline. It is available both in Razor pages and in Razor views. Its usage in views should be limited to very simple processing since views are not supposed to perform complex computations, but should just render the data passed in their models. Complex processing of data contained in the HttpContext
instance should be performed in the controllers that expose the same instance through their HttpContext
property.
The HttpContext
instance contains the following main properties:
Request
contains various data of the request being served, such as the request raw URL, its query parameters, form data if available, and the collection of all cookies sent by the browser.Response
contains data of the response being built. Since the response is automatically created by the framework, the only property of the response that we should use is the list of the cookies to send to the browser.Connection
contains information on the underlying connection with the client, such as its IP address.WebSocket
contains