Freigeben über


ViewResult Class

Represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object.

Inheritance Hierarchy

System.Object
  System.Web.Mvc.ActionResult
    System.Web.Mvc.ViewResultBase
      System.Web.Mvc.ViewResult

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

Syntax

'Declaration
Public Class ViewResult _
    Inherits ViewResultBase
public class ViewResult : ViewResultBase
public ref class ViewResult : public ViewResultBase

The ViewResult type exposes the following members.

Constructors

  Name Description
Public method ViewResult Initializes a new instance of the ViewResult class.

Top

Properties

  Name Description
Public property MasterName Gets the name of the master view (such as a master page or template) to use when the view is rendered.
Public property TempData Gets or sets the TempDataDictionary object for this result. (Inherited from ViewResultBase.)
Public property View Gets or sets the IView object that is rendered to the response. (Inherited from ViewResultBase.)
Public property ViewData Gets or sets the view data ViewDataDictionary object for this result. (Inherited from ViewResultBase.)
Public property ViewEngineCollection Gets or sets the collection of view engines that are associated with this result. (Inherited from ViewResultBase.)
Public property ViewName Gets or sets the name of the view to render. (Inherited from ViewResultBase.)

Top

Methods

  Name Description
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method ExecuteResult When called by the action invoker, renders the view to the response. (Inherited from ViewResultBase.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method FindView Searches the registered view engines and returns the object that is used to render the view. (Overrides ViewResultBase.FindView(ControllerContext).)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

An action result represents a command that ASP.NET performs on behalf of an action method. The ViewResult class implements the abstract ViewResultBase class and is used to render a view. This class contains methods for finding the view to render and for executing the result. This class also contains properties that identify the view to render, the name of the view, the name of the master view, view data, temporary data, and a collection for view engines for the application.

If you want an action method to result in a rendered view, the action method should return a call to the controller's View helper method. The View helper method passes a ViewResult object to the ASP.NET MVC framework, which calls the object's ExecuteResult method.

Examples

The following example shows an action method that uses the controller's View helper method to pass a ViewResult object to the framework, which then executes the result and renders the view.

Public Class MyController
    Inherits System.Web.Mvc.Controller
    Function HelloWorld() As ActionResult
        ViewData("Message") = "Hello World!"
        Return View()
    End Function
End Class
public class MyController : Controller
{
    public ActionResult HelloWorld()
    {
        ViewData["Message"] = "Hello World!";
        return View();
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Web.Mvc Namespace

Other Resources

Understanding the MVC Application Execution Process

Controllers and Action Methods in ASP.NET MVC Applications