Export (0) Print
Expand All

ActionResult Class

Represents the result of an action method.

System.Object
  System.Web.Mvc.ActionResult
    More...

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

public abstract class ActionResult

The ActionResult type exposes the following members.

  NameDescription
Protected methodActionResultInitializes a new instance of the ActionResult class.
Top

  NameDescription
Public methodEquals (Inherited from Object.)
Public methodExecuteResultEnables processing of the result of an action method by a custom type that inherits from the ActionResult class.
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)
Top

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.

Show:
© 2014 Microsoft