Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ActionResult Class
Collapse the table of content
Expand the table of content

ActionResult Class

 

Represents the result of an action method.

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


public abstract class ActionResult

NameDescription
System_CAPS_protmethodActionResult()

Initializes a new instance of the ActionResult class.

NameDescription
System_CAPS_pubmethodEquals(Object)

(Inherited from Object.)

System_CAPS_pubmethodExecuteResult(ControllerContext)

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

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Object.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

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).

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

Return to top
Show:
© 2015 Microsoft