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
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.
You can instantiate the file result classes, but the Controller.File method provides a more convienant way to create file downloads:
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...
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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GET: /Department/Edit/5 | |
public ActionResult Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Department department = _context.Departments.Find(id); | |
if (department == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(department); | |
} |
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
You can instantiate the file result classes, but the Controller.File method provides a more convienant way to create file downloads:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /File/Stream | |
public FileStreamResult Stream() | |
{ | |
var fileContent = new byte[] { Ascii.one, Ascii.Comma, Ascii.two, Ascii.Comma, Ascii.three }; | |
var stream = new MemoryStream(fileContent); | |
var fileStreamResult = new FileStreamResult(stream, mimeType); | |
fileStreamResult.FileDownloadName = "FileStreamExample.csv"; | |
return fileStreamResult; | |
} | |
// File/Stream2 | |
public FileStreamResult Stream2() | |
{ | |
byte[] fileContent = new[] { Ascii.one, Ascii.Comma, Ascii.two, Ascii.Comma, Ascii.three }; | |
return File(new MemoryStream(fileContent), mimeType); | |
} |
Simple Actions
There are a series of actions for returning simple types- EmptyResult
- ContentResult
- JsonResult
- JavascriptResult
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...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ContentResult Content() | |
{ | |
var content = new ContentResult(); | |
content.Content = "<html><body bgcolor=\"red\"><p>This is my HTML content</body></html>"; | |
content.ContentType = "text/html"; | |
return content; | |
} | |
public ContentResult ContentXml() | |
{ | |
var content = new ContentResult(); | |
content.Content = "<books><book>one</book><book>two</book></books>"; | |
content.ContentType = "text/xml"; | |
return content; | |
} |
Returning Http Status
There are three types that can be used to directly return a http status:- HttpStatusCodeResult
- HttpNotFoundResult
- HttpUnauthorizedResult
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public HttpStatusCodeResult Unavailable() | |
{ | |
var status = HttpStatusCode.ServiceUnavailable; | |
var statusCode = new HttpStatusCodeResult(status); | |
return statusCode; | |
} | |
public HttpStatusCodeResult Found() | |
{ | |
var status = HttpStatusCode.Found; | |
var statusCode = new HttpStatusCodeResult(status); | |
return statusCode; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public HttpNotFoundResult NotFound() | |
{ | |
var statusCode = new HttpNotFoundResult(); | |
return statusCode; | |
} | |
public HttpUnauthorizedResult Unauthorized() | |
{ | |
var statusCode = new HttpUnauthorizedResult(); | |
return statusCode; | |
} |