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.

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 information on a possible WebSocket connection established with the client. WebSocket handling is beyond the purpose of this course. The .Net preferred way for handling WebSocket connections is the SignalR framework that is an independent part of ASP.NET Core.
  • User contains information on the logged user. If there is no logged user, this property contains an anonymous user.
  • Features is a Type/object dictionary that contains information on the features of the web server environment, and on the software modules used, such as the hosting operating system, the kind of authentication engine being used, and so on. We should rarely use it.
  • Items is an object/object dictionary where various application modules can share custom data. It is useful for designing custom pipeline modules, but we should not use it when implementing applications, since ASP.NET Core has more powerful tools for sharing business data.

User is very useful both in views and in pages since we can use it to adapt the content of the page to the user privileges. For instance, the snippet below shows part of the user interface only if the user is an administrator:

Get hands-on with 1200+ tech skills courses.