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 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.

Controller

Action Result

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 types of action results.

Action Result 
Figure 2. Types of Action Results

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

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 the page.

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

View Result

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

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

View Result is a class and is derived from the “ViewResultBase” class. “ViewResultBase” is derived from Action Result. The view Result base class is an Action Result. Action Result is a base class of different action results.

View Result

ViewResultBase

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

Partial View Result

Partial View Result returns the result to the Partial View page. A partial view is one of the views that we can call inside the Normal view page.

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

Normal view page

We should create a Partial view inside the 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 the Action Result.

Redirect Result

Redirect result is returning the result to a 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 the Redirect to Action method. If not mentioned, the Controller name redirects to a mentioned action method in the current Controller. Suppose the 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 a simple text file format and key-value pairs. If we call the action method, using Ajax, it should return a 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

Output

While returning more data in JSON format, there is a need to mention the maximum length. Assign the maximum length of data Using the “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 a different file format view page when we implement the file download concept in MVC using file result. Simple examples of file results are shown below.

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

Output

Output XML

Content Result

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

Example

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

Output

HTML Formate

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

Output

Welcome All

Conclusion

This article explains about Action Results. It is very useful for new MVC learners and students.


Similar Articles