Action Results in ASP.NET MVC
Action Results in ASP.NET
An action method can produce a variety of results. For example, an action method can just act as a web service and return a plain string or a JSON string in response to a request. Likewise, an action method can determine that there’s no content to return or that a redirect to another URL is required. The ActionResult class just represents the ASP.NET MVC infrastructure for implementing this programming aspect.
Inside the ActionResult class
An action method typically returns an object of the type ActionResult. The type ActionResult is not a data container, though. More precisely, it is an abstract class that offers a common programming interface to execute some further operations on behalf of the action method.
public abstract class ActionResult { protected ActionResult() { } public abstract void ExecuteResult(ControllerContext context); }
Predefined ActionResult types
ContentResult: Sends raw content to the browser. The ExecuteResult method of this class serializes any content it receives.
EmptyResult: Sends no content to the browser. The ExecuteResult method of this class does nothing.
FileContentResult: Sends the content of a file to the browser. The content of the file is expressed as a byte array. The ExecuteResult method simply writes the array of bytes to the output stream.
FilePathResult: Sends the content of a file to the browser. The file is identified via its path and content type. The ExecuteResult method calls the TransmitFile method on HttpResponse.
FileStreamResult: Sends the content of a file to the browser. The content of the file is represented through a Stream object. The ExecuteResult method copies from the provided file stream to the output stream.
HttpNotFoundResult: Sends an HTTP 404 response code to the browser. The HTTP status code identifies a request that failed because the requested resource was not found.
HttpUnauthorizedResult: Sends an HTTP 401 response code to the browser. The HTTP status code identifies an unauthorized request.
JavaScriptResult: Sends JavaScript text to the browser. The ExecuteResult method of this class writes out the script and sets the content type accordingly.
JsonResult: Sends a JSON string to the browser. The ExecuteResult method of this class sets the content type to the application or JSON and invokes the JavaScriptSerializer class to serialize any provided managed object to JSON.
PartialViewResult: Sends HTML content to the browser that represents a fragment of the whole page view. A partial view in ASP.NET MVC is a concept very close to user control in WebForms.
RedirectResult: Sends an HTTP 302 response code to the browser to redirect the browser to the specified URL. The ExecuteResult method of this class just invokes Response. Redirect.
RedirectToRouteResult: Like RedirectResult, it sends an HTTP 302 code to the browser and the new URL to which to navigate. The difference is in the logic and input data employed to determine the target URL. In this case, the URL is built based on action/controller pairs or route names.
ViewResult: Sends HTML content to the browser that represents a full pageview.