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
- public ViewResult ViewResultSample()
- {
- return View();
- }
Step 4
Create a new view of its action.
- @{
- ViewBag.Title = "ViewResultSample";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>ViewResult Sample</h2>
Step 5
Return the view from the index,
- @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
- public PartialViewResult PartialViewResultSample()
- {
- return PartialView("_PartialViewResultSample");
- }
Step 4
Create a partial View named _PartialViewResultSample
Do not forget to check the checkbox "Create a partial view"
Step 5
Call its method from the Index.
- @Html.Action("PartialViewResultSample")
RedirectResult - Redirects to another action method by using its URL.
Step 3
Create a new method of type RedirectResult named RedirectResultSample.
- public RedirectResult RedirectResultSample()
- {
- return Redirect("Index");
- }
Step 4
Return the object calling the view to be redirected.
- @Html.ActionLink("RedirectResult Sample", "RedirectResultSample", "ActionResults")
RedirectToRouteResult - Redirects to another action method.
Step 3
Create a new method of type RedirectToRouteResult named RedirectToRouteResultSample.
- public RedirectToRouteResult RedirectToRouteResultSample()
- {
- return RedirectToRoute(new { Controller = "Home", Action = "Index" });
- }
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
- public ContentResult ContentResultSample()
- {
- return Content("ContentResult Sample");
- }
Step 4
Call the ContentResultSample from the view.
- @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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 5
Create a new method of type JsonResult named JsonResultSample.
- public JsonResult JsonResultSample()
- {
- Dictionary<int, string> Clients = new Dictionary<int, string>();
- Clients.Add(1, "Client1");
- Clients.Add(2, "Client2");
- Clients.Add(3, "Client3");
- Clients.Add(4, "Client4");
-
- return Json(Clients.ToList(), JsonRequestBehavior.AllowGet);
- }
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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 5
Create a new method of type JavaScriptResult named JavaScriptResultSample
- public JavaScriptResult JavaScriptResultSample()
- {
- string jsData = @" $('#updateTime').html('" + DateTime.Now.ToLocalTime() + "')";
- return JavaScript(jsData);
- }
Step 6
Call the JavascriptResultSample method from the indexed View.
- @Ajax.ActionLink("JavascriptResult Sample", "JavaScriptResultSample", "ActionResults", new AjaxOptions
- {
- HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updateTime"
- })
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
- public HttpStatusCodeResult HttpStatusCodeResultSample()
- {
- return new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed, "HttpStatusCodeResult Sample");
- }
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.
- public HttpUnauthorizedResult HttpUnauthorizedResultSample()
- {
- return new HttpUnauthorizedResult("HttpUnauthorizedResult Sample");
- }
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.
- public HttpNotFoundResult HttpNotFoundResultSample()
- {
- return new HttpNotFoundResult("HttpNotFoundResult Sample");
- }
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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 5
Create a new method of type FileResult named FileResultSample
- public FileResult FileResultSample()
- {
-
- byte[] data = Encoding.UTF8.GetBytes("file result sample");
- return File(data, "text/plain");
- }
Step 6
Call the FileResultSample method from the index View.
- @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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 3
Create a new method of type FileContentResult named FileContentResultSample
- public FileContentResult FileContentResultSample()
- {
- byte[] data = Encoding.UTF8.GetBytes("file content result sample");
- return new FileContentResult(data, "text/plain");
- }
Step 6
Call the FileContentResultSample method from the index View
- @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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 5
Create a new method of type FilePathResult named FilePathResultSample.
- public FilePathResult FilePathResultSample()
- {
- return new FilePathResult("~\\Content\\bootstrap.css", "application/css");
- }
Step 6
Call the FilePathResultSample method from the index View.
- @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.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Step 5
Create a new method of type FileStreamResult named FileStreamResultSample.
- public FileStreamResult FileStreamResultSample()
- {
- byte[] data = Encoding.UTF8.GetBytes("file stream result sample");
- var stream = new MemoryStream(data);
- var fileStreamResult = new FileStreamResult(stream, "text/plain");
- fileStreamResult.FileDownloadName = "FileStreamResultSample.txt";
- return fileStreamResult;
- }
Step 6
Call the FileStreamResultSample method from the index View.
- @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.
- public EmptyResult EmptyResultSample()
- {
- return new EmptyResult();
- }
Congratulations! You have gone through all the action results on MVC 5.