Search⌘ K
AI Features

Views Predefined Properties

Explore how to use predefined properties in ASP.NET Core MVC views to access HttpContext, ViewContext, TempData, and other utilities. Understand how these properties help manage request data, user information, URL building, and temporary messages to create dynamic and responsive Razor templates.

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
...