ActionResult Class

 

Represents the result of an action method.

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

Inheritance Hierarchy

System.Object
  System.Web.Mvc.ActionResult
    System.Web.Mvc.ContentResult
    System.Web.Mvc.EmptyResult
    System.Web.Mvc.FileResult
    System.Web.Mvc.HttpStatusCodeResult
    System.Web.Mvc.JavaScriptResult
    System.Web.Mvc.JsonResult
    System.Web.Mvc.RedirectResult
    System.Web.Mvc.RedirectToRouteResult
    System.Web.Mvc.ViewResultBase

Syntax

public abstract class ActionResult
public ref class ActionResult abstract 
[<AbstractClass>]
type ActionResult = class end
Public MustInherit Class ActionResult

Constructors

Name Description
System_CAPS_protmethod ActionResult()

Initializes a new instance of the ActionResult class.

Methods

Name Description
System_CAPS_pubmethod Equals(Object)

(Inherited from Object.)

System_CAPS_pubmethod ExecuteResult(ControllerContext)

Enables processing of the result of an action method by a custom type that inherits from the ActionResult class.

System_CAPS_protmethod Finalize()

(Inherited from Object.)

System_CAPS_pubmethod GetHashCode()

(Inherited from Object.)

System_CAPS_pubmethod GetType()

(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethod ToString()

(Inherited from Object.)

Remarks

Action methods typically return a result that is known as an action result. The ActionResult class is the base class for all action results. You decide which type of action result to return based on the task that the action method is performing.

A common action result is obtained by calling the View method, which returns an instance of the ViewResult class. The ViewResult derives from ActionResult. In that case, the return type of the action method is ActionResult, but an instance of the ViewResult class is returned, as shown below.

public ActionResult Index()
{
    return View();
}

It is helpful to set the return type to ActionResult because in more complicated action methods, you may need to return different types of results for different scenarios. For example, the registration action method from the default MVC template, redirects the users if registration is successful, but displays the error messages if registration fails.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser() { UserName = model.UserName };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInAsync(user, isPersistent: false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            AddErrors(result);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

You can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream.

The following table shows the built-in action result types and the action helper methods that return them.

Action Result

Helper Method

Description

ViewResult

View

Renders a view as a Web page.

PartialViewResult

PartialView

Renders a partial view, which defines a section of a view that can be rendered inside another view.

RedirectResult

Redirect

Redirects to another action method by using its URL.

RedirectToRouteResult

RedirectToAction or RedirectToRoute

Redirects to another action method.

ContentResult

Content

Returns a user-defined content type.

JsonResult

Json

Returns a serialized JSON object.

JavaScriptResult

JavaScript

Returns a script that can be executed on the client.

HttpStatusCodeResult

(None)

Returns a specific HTTP response code and description.

HttpUnauthorizedResult

(None)

Returns the result of an unauthorized HTTP request.

HttpNotFoundResult

HttpNotFound

Indicates the requested resource was not found.

FileResult

File

Returns binary output to write to the response.

FileContentResult

Controller.File(Byte[], String) or Controller.File(Byte[], String, String)

Sends the contents of a binary file to the response.

FilePathResult

Controller.File(String, String) or Controller.File(String, String, String)

Sends the contents of a file to the response.

FileStreamResult

Controller.File(Stream, String) or Controller.File(Stream, String, String)

Sends binary content to the response through a stream.

EmptyResult

(None)

Represents a return value that is used if the action method must return a null result (void).

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

System.Web.Mvc Namespace

Return to top