MVC 5 - Action Results Explained

This article is going to explain the usage of every each MVC 5 action result.

This article is going to explain the usage of every each MVC 5 action result. Action results define what type of information is going to be received by the view. So, depending on the input that you have you may want to execute a Javascript code, redirect to another view/action, or even send an empty result. To do so, you need to use the correct action result or even create a custom one. Below, you will find a description and a working example of the most used Action Result types.

NuGet packages Used for this solution,

  • https://www.nuget.org/packages/jQuery
  • https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax 

Step 1

Create a new MVC Project.

Step 2

Add an empty controller named ActionResultsController

Now, check the next steps in the respective Action Result.

ViewResult - Renders a view as a Web page.

Step 3

Create a new method of type ViewResult named ViewResultSample 

  1. public ViewResult ViewResultSample()    
  2.       {    
  3.           return View();    
  4.       }   

Step 4

Create a new view of its action.

  1. @{    
  2.     ViewBag.Title = "ViewResultSample";    
  3.     Layout = "~/Views/Shared/_Layout.cshtml";    
  4. }    
  5.     
  6. <h2>ViewResult Sample</h2>    

Step 5

Return the view from the index,

  1. @Html.ActionLink("ViewResult Sample""ViewResultSample""ActionResults")    

PartialViewResult - Renders a partial view, which defines a section of a view that can be rendered inside another view

Step 3

Create a new method of type PartialViewResult named PartialViewResultSample

  1. public PartialViewResult PartialViewResultSample()    
  2.      {    
  3.          return PartialView("_PartialViewResultSample");    
  4.      }    

Step 4

Create a partial View named _PartialViewResultSample

Do not forget to check the checkbox "Create a partial view"

 partial view

Step 5

Call its method from the Index.

  1. @Html.Action("PartialViewResultSample")    

RedirectResult - Redirects to another action method by using its URL.

Step 3

Create a new method of type RedirectResult named RedirectResultSample.

  1. public RedirectResult RedirectResultSample()    
  2. {    
  3.     return Redirect("Index");    
  4. }    

Step 4

Return the object calling the view to be redirected.

  1. @Html.ActionLink("RedirectResult Sample""RedirectResultSample""ActionResults")    

RedirectToRouteResult - Redirects to another action method.

Step 3

Create a new method of type RedirectToRouteResult named RedirectToRouteResultSample.

  1. public RedirectToRouteResult RedirectToRouteResultSample()    
  2.     {    
  3.         return RedirectToRoute(new { Controller = "Home", Action = "Index" });    
  4.     }    

Step 4

Return the object passing the route values.

ContentResult - Returns a user-defined content type.

Step 3

Create a new method of type ContentResult named ContentResultSample

  1. public ContentResult ContentResultSample()    
  2. {    
  3.     return Content("ContentResult Sample");    
  4. }    

Step 4

Call the ContentResultSample from the view.

  1. @Html.ActionLink("ContentResult Sample""ContentResultSample""ActionResults")    

JsonResult - Returns a serialized JSON object.

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>    

Step 5

Create a new method of type JsonResult named JsonResultSample.

  1. public JsonResult JsonResultSample()    
  2.     {    
  3.         Dictionary<int, string> Clients = new Dictionary<int, string>();    
  4.         Clients.Add(1, "Client1");    
  5.         Clients.Add(2, "Client2");    
  6.         Clients.Add(3, "Client3");    
  7.         Clients.Add(4, "Client4");    
  8.     
  9.         return Json(Clients.ToList(), JsonRequestBehavior.AllowGet);    
  10.     }    

Step 6

Call the JsonResultSample method from the indexed View.

JavaScriptResult - Returns a script that can be executed on the client.

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>    

Step 5

Create a new method of type JavaScriptResult named JavaScriptResultSample

  1. public JavaScriptResult JavaScriptResultSample()    
  2.    {    
  3.        string jsData = @" $('#updateTime').html('" + DateTime.Now.ToLocalTime() + "')";    
  4.        return JavaScript(jsData);    
  5.    }    

Step 6

Call the JavascriptResultSample method from the indexed View.

  1. @Ajax.ActionLink("JavascriptResult Sample""JavaScriptResultSample""ActionResults"new AjaxOptions  
  2. {  
  3.     HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updateTime"  
  4. })  

HttpStatusCodeResult - Returns a specific HTTP response code and description

Step 3

Create a new method of type HttpStatusCodeResult named HttpStatusCodeResultSample

Step 4

Return an object of type HttpStatusCodeResult

  1. public HttpStatusCodeResult HttpStatusCodeResultSample()    
  2. {    
  3.     return new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed, "HttpStatusCodeResult Sample");    
  4. }    

HttpUnauthorizedResult - Returns the result of an unauthorized HTTP request

Step 3

Create a new method of type HttpUnauthorizedResult named HttpUnauthorizedResultSample.

Step 4

Return an object of type HttpUnauthorizedResult.

  1. public HttpUnauthorizedResult HttpUnauthorizedResultSample()    
  2.    {    
  3.        return new HttpUnauthorizedResult("HttpUnauthorizedResult Sample");    
  4.    }    

HttpNotFoundResult - Indicates the requested resource was not found

Step 3

Create a new method of type HttpNotFoundResult named HttpNotFoundResultSample.

Step 4

Return an object of type HttpNotFoundResult.

  1. public HttpNotFoundResult HttpNotFoundResultSample()    
  2. {    
  3.     return new HttpNotFoundResult("HttpNotFoundResult Sample");    
  4. }   

FileResult - Returns binary output to write to the response

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>    

Step 5

Create a new method of type FileResult named FileResultSample

  1. public FileResult FileResultSample()    
  2. {    
  3.     
  4.     byte[] data = Encoding.UTF8.GetBytes("file result sample");    
  5.     return File(data, "text/plain");    
  6. }    

Step 6

Call the FileResultSample method from the index View. 

  1. @Html.ActionLink("FileResult Sample""FileResultSample""ActionResults")   

FileContentResult - Sends the contents of a binary file to the response

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.  

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>  

Step 3

Create a new method of type FileContentResult named FileContentResultSample

  1. public FileContentResult FileContentResultSample()    
  2. {    
  3.     byte[] data = Encoding.UTF8.GetBytes("file content result sample");    
  4.     return new FileContentResult(data, "text/plain");    
  5. }    

Step 6

Call the FileContentResultSample method from the index View

  1. @Html.ActionLink("FileContentResult Sample""FileContentResultSample""ActionResults")    

FilePathResult - Sends the contents of a file to the response

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>    

Step 5

Create a new method of type FilePathResult named FilePathResultSample.

  1. public FilePathResult FilePathResultSample()    
  2. {    
  3.     return new FilePathResult("~\\Content\\bootstrap.css""application/css");    
  4. }    

Step 6

Call the FilePathResultSample method from the index View.

  1. @Html.ActionLink("FilePathResult Sample""FilePathResultSample""ActionResults")    

FileStreamResult - Sends binary content to the response through a stream

Step 3

Make sure that you have Ajax and jQuery installed.

Step 4

Include jQuery and Ajax Libraries into the view.

  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  2. <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>   

Step 5

Create a new method of type FileStreamResult named FileStreamResultSample.

  1. public FileStreamResult FileStreamResultSample()    
  2. {    
  3.     byte[] data = Encoding.UTF8.GetBytes("file stream result sample");    
  4.     var stream = new MemoryStream(data);    
  5.     var fileStreamResult = new FileStreamResult(stream, "text/plain");    
  6.     fileStreamResult.FileDownloadName = "FileStreamResultSample.txt";    
  7.     return fileStreamResult;    
  8. }    

Step 6

Call the FileStreamResultSample method from the index View.

  1. @Html.ActionLink("FileStreamResult Sample""FileStreamResultSample""ActionResults")    

EmptyResult

Represents a return value that is used if the action method must return a null result (void).

Step 3

Create a new method of type EmptyResult named EmptyResultSample

Step 4

Return an object of type EmptyResult.

  1. public EmptyResult EmptyResultSample()    
  2.   {    
  3.       return new EmptyResult();    
  4.   }    

Congratulations! You have gone through all the action results on MVC 5.