liveBook · Manning

ViewResult

As explained in ASP.NET Core in Action, 3e

ViewResult is an ASP.NET Core MVC action result that renders a Razor view using an optional view name and model.

ViewResult is an ASP.NET Core MVC action result that tells the framework to render a Razor view. It can carry an optional view name and a view model: a plain CLR object containing the data passed to the template. section 19.4

A controller action typically uses ViewResult in one of these ways:

  • Return View() to let MVC select a view by convention. section 19.6
  • Return View(model) to select the conventional view and pass it a view model. section 19.2

Returning a view with a model

public IActionResult Index() { var model = new ToDoListModel(); return View(model); } MVC receives the ViewResult, locates the Razor view, passes the model to it, and executes the template to generate HTML. section 19.4

Without an explicit name, MVC looks for a view named after the action method, under a folder named after the controller or in the Shared folder. Razor Pages use PageResult instead: their associated view is fixed and receives the PageModel. section 19.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains optional names, models, runtime view lookup, and Razor rendering, but does not detail lookup heuristics.
Examples
Uses HomeController, ToDoController, Index(), named ViewToDo, and ErrorViewModel examples.
What it assumes
Assumes basic C# actions and MVC controllers while establishing view models and Razor view selection.
What you can do after
Apply it via the View() helper to render conventional or named Razor views with optional models.
How it connects
Connects MVC controllers, action methods, Razor views, view models, conventional routing, and PageResult.

Also explained in

Compare how 2 books explain ViewResult →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.