Share via


Different Types Of Action Results In ASP.NET MVC

Introduction

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

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.

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

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 the page through which class is defined in the model. View page is a simple HTML page. Here view page has “.cshtm” extension.

public ViewResult About()  
       {  
            ViewBag.Message = "Your application description page.";  
            return View();  
   
       }

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.


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

Partial View Result

Partial View Result is returning the result to a Partial view page. Partial view is one of the views that we can call inside the Normal view page.
   

public PartialViewResult Index()  
   {  
   return PartialView("_PartialView");  
   }

We should create a Partial view inside a shared folder, otherwise, we cannot access the Partial view. The diagram is shown above the Partial view page and Layout page because the 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 the specific URL. It is rendered to the page by URL. If it gives the wrong URL, it will show 404-page errors.
 

public RedirectResult Index()  
    {  
    return Redirect("Home/Contact");  
    }

Redirect to Action Result

Redirect to Action result is returning the result to a specified controller and action method. The controller name is optional in Redirect to the Action method. If not mentioned, Controller name redirects to a mentioned action method in the current Controller. Suppose action name is not available but mentioned in the current controller, then it will show a 404-page error.

public ActionResult Index()  
{  
return RedirectToAction("Login", "Account");   
}

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 the action method, using Ajax, it should return Json result.
   

public ActionResult Index()  
    {  
       var persons = new  List<Person1>  
           {  
            new Person1{Id=1, FirstName="Harry", LastName="Potter"},  
                  new Person1{Id=2, FirstName="James", LastName="Raj"}  
           };  
           return Json(persons, JsonRequestBehavior.AllowGet);   
      }

Output

While returning more data in Json format, there is a need to mention the maximum length. Assign maximum length of data Using “MaxJsonLength” property.
   

public ActionResult Index()  
    {  
    var persons = new  List<Person1>  
           {  
            new Person1{Id=1, FirstName="Harry", LastName="Potter"},  
                  new Person1{Id=2, FirstName="James", LastName="Raj"}  
                   
    };  
       
           var jsonResult = Json(persons, JsonRequestBehavior.AllowGet);  
           jsonResult.MaxJsonLength = int.MaxValue;  
           return jsonResult;  
    }

 

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:
   

public ActionResult Index()  
    {  
    return File("Web.Config", "text");   
    }

Output

Content Result

The 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

   

public ActionResult Contact()  
    {  
    ViewBag.Message = "Your contact page.";  
       
           return View();  
    }

Output

   

public ActionResult Index()  
    {  
    return Content("<script>alert('Welcome To All');</script>");  
    }

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.