ActionResults in ASP.NET MVC 5

In ASP.NET MVC 5 ActionResult classes are used to return data from the controller classes to the views. There are 15 different sub classes that can be used in various situations depending on your scenario...

Its usual to return the base class ActionResult, rather than a specific action result type because you may want to return different actions in different situations. The example below is actually the standard ASP.NET scaffolding for the edit entity framework model item.


Usually, when creating ActionResult objects, you use one of the Controller base class helper methods as shown above. There is a good overview of the methods available for each result type on the ActionResult MSDN page.

Please note that a sample application containing demonstrations can be found at github.com/johnmmoss and is also hosted on Appharbour at mvcsampleapp.apphb.com

Displaying a Page

The controller's View method can be used to create a ViewResult which is returned by the executing action to display a page. By convention the name of the view that is loaded is the same as the name of the executing action, so the index controller method (or action) will map to the index view in the views folder. The example above demonstrates the basics.

The Controller.View method provides overloads for passing a model to strongly typed views, and also for specifying the name of the view to load in case it differs from the executing actions name.

The PartialViewResult type is used to return markup without the layout page loaded around it. it has a corresponding This is most useful when using JavaScript from the browser to call back into the server.

Downloading Files

When it comes to download files, the framework again provides a base FileResult class with some overloads. The three subsequent sub classes are:
  • FileContentResult 
  • FilePathResult
  • FileStreamResult
So, FileContentResult is used to return a file from a byte array, FilePathResult when you have a file on disk and you provide the path to it, and FileStreamResult is used when you have a stream that you want to use to create the download file.

You can instantiate the file result classes, but the Controller.File method provides a more convienant way to create file downloads:


Simple Actions

There are a series of actions for returning simple types
  • EmptyResult
  • ContentResult
  • JsonResult
  • JavascriptResult
EmptyResult is useful to use when you are running async actions and dont actually have anything to return. Its an implementation of the Null object pattern.

ContentResult can be used to return any type of content to the user. As you can set the mime type, this type is really quite flexible, but because there are other types that cater for most common cases it actually doesnt get used that much. In the example below, the Content type is used to return first a simple Html page, and then a simple Xml document...


Returning Http Status

There are three types that can be used to directly return a http status:
  • HttpStatusCodeResult
  • HttpNotFoundResult
  • HttpUnauthorizedResult
If you want to return a particular Http status code, you can use the HttpStatusCodeResult class to return any status. The HttpStatusCode enum is a useful type, which can be passed into the constructor. This result type does not actually have a Controller class helper method.

There are two specialized classes that sub class HttpStatusCodeResult. These are HttpNotFoundResult and HttpUnauthorizedResult. There is a Controller.HttpNotFound method which can be used to as a helper to create not found statuses, but unauthorized does not have one.






Popular posts from this blog

A Simple 3 Layer Architecture

Parsing Visual Studio Files with Powershell