In all the articles, I have used ActionResult while returning a view. But in reality, an MVC View actually returns a ViewResult.
Now, the question arises then how we were capable of returning view even when return type was ActionResult. Leave this question for a moment and come to the below scenarios.
For real-world application clients can be anyone. Meaning a request to the controller can come directly from the browser via URL or UI or it may come from any javascript client like Jquery (Ajax).
• A browser always expects an Html result back as a response while
• JavaScript most probably requires JSON response.
Now, with the single return type, two different responses can't be sent. And only because of the difference in the return type, you will end up creating two different methods for the same purpose. It will cause duplicate code in your application.
public ActionResult GetEmployees()
{
List<employee> employeeList = new List<employee>() {
new Employee(){Id=1,Name="Michael",Gender="Male",Age=23},
new Employee(){Id=2,Name="John",Gender="Male",Age=26},
new Employee(){Id=3,Name="Lucy",Gender="female",Age=20},
};
return View(employeeList);
}
public ActionResult GetEmployeeList()
{
List<employee> employeeList = new List<employee>() {
new Employee(){Id=1,Name="Michael",Gender="Male",Age=23},
new Employee(){Id=2,Name="John",Gender="Male",Age=26},
new Employee(){Id=3,Name="Lucy",Gender="female",Age=20},
};
return Json(employeeList,JsonRequestBehavior.AllowGet);
}
To solve such problem in MVC ,we have ActionResult.
ActionResult is actually parent of all other result.And every other response type are derived from ActionResult.So,it can be used to return any response like JsonResult,ViewResult,ContentResult etc.
public ActionResult GetEmployees()
{
string type = Request.QueryString["type"];
List<employee> employeeList = new List<employee>() {
new Employee(){Id=1,Name="Michael",Gender="Male",Age=23},
new Employee(){Id=2,Name="John",Gender="Male",Age=26},
new Employee(){Id=3,Name="Lucy",Gender="female",Age=20},
};
if(type=="Json")
{
return Json(employeeList, JsonRequestBehavior.AllowGet);
}
return View(employeeList);
}
Now,As you can observe if anyone asks for json type JsonResult will be sent else ViewResult will be sent.
Now I think you got your answer that how were we able to return View with ActionResult as return type.
If we summerize , then we could say that all public methods of the Controller class in Asp.Net MVC are called Action Methods , The Action method can return any result type , like EmptyResult, JsonResult , ViewResult , PartialViewResult , RedirectResult , RedirectToRouteResult etc ,and it doesn't make any sense to remember all these Result Types and return accordingly , so We have a Parent Type as ActionResult and all other Result Types inherit from this parent type , so that instead of returning the exact Result type we can now simply return an ActionResult and it will automatically be casted into appropriate Result Type at run time by the virtue of Polymorphism.