...

/

Building Responses

Building Responses

In this lesson, we will gain more insights on all response types an action method can return.

We already said that action methods return IAsyncResult implementations, which are then executed by the framework after possible action filters have been applied. This lesson classifies all predefined IAsyncResult implementations and gives more details about each of them.

ViewResult and View method

The ViewResult implementation is returned when an action method calls the View method when we write return View(...);. The View method has several overloads. Each overload can specify whether or not which of the paths of the view to invoke and which ViewModel to pass it. If no view name is specified the framework tries to find a view with the same name of the action method, while if no ViewModel is specified a null ViewModel is passed to the view.

In order to retrieve a view, the View method adds the .cshtml extension to the view name it receives and then interprets it as paths. If the path is relative the view is first searched in a folder with the same name of the controller. For instance, Home for the HomeController, which is under the Views folder. If the view is not found there it is searched under the “Shared” folder, which is always under the Views folder. Absolute paths that start with a / are not searched in the hard disk root, but in the application root folder.

When a view is invoked it is passed the current request HttpContext, the controller TempData, and the Controller ViewData, plus the whole controller ModelState that ...