ActionResult Class
Represents the result of an action method.
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
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
| Name | Description | |
|---|---|---|
![]() | ActionResult() | Initializes a new instance of the ActionResult class. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | (Inherited from Object.) |
![]() | ExecuteResult(ControllerContext) | Enables processing of the result of an action method by a custom type that inherits from the ActionResult class. |
![]() | Finalize() | (Inherited from Object.) |
![]() | GetHashCode() | (Inherited from Object.) |
![]() | GetType() | (Inherited from Object.) |
![]() | MemberwiseClone() | (Inherited from Object.) |
![]() | ToString() | (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 |
|---|---|---|
Renders a view as a Web page. | ||
Renders a partial view, which defines a section of a view that can be rendered inside another view. | ||
Redirects to another action method by using its URL. | ||
Redirects to another action method. | ||
Returns a user-defined content type. | ||
Returns a serialized JSON object. | ||
Returns a script that can be executed on the client. | ||
(None) | Returns a specific HTTP response code and description. | |
(None) | Returns the result of an unauthorized HTTP request. | |
Indicates the requested resource was not found. | ||
Returns binary output to write to the response. | ||
Controller.File(Byte[], String) or Controller.File(Byte[], String, String) | Sends the contents of a binary file to the response. | |
Controller.File(String, String) or Controller.File(String, String, String) | Sends the contents of a file to the response. | |
Controller.File(Stream, String) or Controller.File(Stream, String, String) | Sends binary content to the response through a stream. | |
(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.
.jpeg?cs-save-lang=1&cs-lang=csharp)
.jpeg?cs-save-lang=1&cs-lang=csharp)