Compartilhar via


Controller.View Method

Creates a ViewResult object that renders a view to the response.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Syntax

'Declaration
Protected Friend Function View As ViewResult
protected internal ViewResult View()
protected public:
ViewResult^ View()

Return Value

Type: System.Web.Mvc.ViewResult
The view result that renders a view to the response.

Remarks

This method overload of the View class returns a ViewResult object that has an empty ViewName property. If you are writing unit tests for controller actions, take into account the empty ViewName property for unit tests that do not take a string view name.

At run time, if the ViewName property is empty, the current action name is used in place of the ViewName property.

This method returns the view that matches the action result name from the Views subfolder that in turn matches the controller name. For example, if you are using the default view engine, the action result TestJson in a controller named PRGController returns the following view:

Views\PRG\TestJson.aspx

Examples

The following example shows how to return a view from an action method. Both action methods return the TestJson.aspx page from the Views subfolder that matches the controller name. The TestJson method defaults to returning the TestJson.aspx page. The TestJsonContent method explicitly returns the TestJson.aspx page.

public ActionResult TestJson() {
    return View();
}
public ActionResult TestJsonContent() {
    return View("TestJson");
}
Public Function TestJson() As ActionResult 
    Return View() 
End Function 
Public Function TestJsonContent() As ActionResult 
    Return View("TestJson") 
End Function 

See Also

Reference

Controller Class

View Overload

System.Web.Mvc Namespace