Synopsis
- Introduction
- Definition
- Types of Action Result
- 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.
- 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.
![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.
- public PartialViewResult Index()
- {
- return PartialView("_PartialView");
- }
![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.
- 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. 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.
- 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 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
![Output]()
While returning more data in Json format, there is a need to mention 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
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.