Different Types Of Action Results In ASP.NET MVC

Synopsis

  1. Introduction
  2. Definition
  3. Types of Action Result
  4. Conclusion

Introduction

In ASP.NET, MVC has different types of Action Results. Each action result returns a different format of output. A programmer uses different action results to get expected output. Action Results return the result to view the page for the given request.

result
Definition

Action Result is a result of action methods or return types of action methods. Action result is an abstract class. It is a base class for all type of action results. 

Types
                                             Figure 2: Types of Action Result

The diagram shown below describes about abstract class of Action Result. There are two methods in Action Result. One is ActionResult() and another one is ExecuteResult().

class
Types of Action Results

There are different Types of action results in ASP.NET MVC. Each result has a different type of result format to view page.

  • View Result
  • Partial View Result
  • Redirect Result
  • Redirect To Action Result
  • Redirect To Route Result
  • Json Result
  • File Result
  • Content Result

View Result

View result is a basic view result. It returns basic results to view page. View result can return data to view page through which class is defined in the model. View page is a simple HTML page. Here view page has “.cshtm” extension.

  1. public ViewResult About()  
  2.        {  
  3.             ViewBag.Message = "Your application description page.";  
  4.             return View();  
  5.   
  6.        }  
View Result is a class and is derived from “ViewResultBase” class. “ViewResultBase” is derived from Action Result. View Result base class is an Action Result. Action Result is a base class of different action result.

result
result

View Result class is inherited from Action Result class by View Result Base class. The diagram shown above describes inheritance of Action Results.

Partial View Result

Partial View Result is returning the result to Partial view page. Partial view is one of the views that we can call inside Normal view page.
  1. public PartialViewResult Index()  
  2. {  
  3. return PartialView("_PartialView");  
  4. }  

partial
We should create a Partial view inside shared folder, otherwise we cannot access the Partial view. The diagram is shown above the Partial view page and Layout page because layout page is a Partial view. Partial View Result class is also derived from Action Result.

Redirect Result

Redirect result is returning the result to specific URL. It is rendered to the page by URL. If it gives wrong URL, it will show 404 page errors.
  1. public RedirectResult Index()  
  2. {  
  3. return Redirect("Home/Contact");  
  4. }  
Redirect to Action Result

Redirect to Action result is returning the result to a specified controller and action method. Controller name is optional in Redirect to Action method. If not mentioned, Controller name redirects to a mentioned action method in current Controller. Suppose action name is not available but mentioned in the current controller, then it will show 404 page error.
  1. public ActionResult Index()  
  2. {  
  3. return RedirectToAction("Login""Account");  
  4. }  
Json Result

Json result is a significant Action Result in MVC. It will return simple text file format and key value pairs. If we call action method, using Ajax, it should return Json result.
  1. public ActionResult Index()  
  2. {  
  3. var persons = new List<Person1>  
  4.        {  
  5.         new Person1{Id=1, FirstName="Harry", LastName="Potter"},  
  6.               new Person1{Id=2, FirstName="James", LastName="Raj"}  
  7.        };  
  8.        return Json(persons, JsonRequestBehavior.AllowGet);  
  9.   }  
Output

Output

While returning more data in Json format, there is a need to mention maximum length. Assign maximum length of data Using “MaxJsonLength” property.
  1. public ActionResult Index()  
  2. {  
  3. var persons = new List<Person1>  
  4.        {  
  5.         new Person1{Id=1, FirstName="Harry", LastName="Potter"},  
  6.               new Person1{Id=2, FirstName="James", LastName="Raj"}  
  7.               
  8. };  
  9.   
  10.        var jsonResult = Json(persons, JsonRequestBehavior.AllowGet);  
  11.        jsonResult.MaxJsonLength = int.MaxValue;  
  12.        return jsonResult;  
  13. }  
File Result

File Result returns different file format view page when we implement file download concept in MVC using file result. Simple examples for file result are shown below:
  1. public ActionResult Index()  
  2. {  
  3. return File("Web.Config""text");  
  4. }  
Output

Output

Content Result

Content result returns different content's format to view. MVC returns different format using content return like HTML format, Java Script format and any other format.

Example
  1. public ActionResult Contact()  
  2. {  
  3. ViewBag.Message = "Your contact page.";  
  4.   
  5.        return View();  
  6. }  
Output

Output

  1. public ActionResult Index()  
  2. {  
  3. return Content("<script>alert('Welcome To All');</script>");  
  4. }  
Output

Output

Conclusion

This article explains about Action Results. It is very useful for new MVC learners and students. Next article will briefly explain about File Action Result.

Up Next
    Ebook Download
    View all
    Learn
    View all