So you have hit this blog note because you are confused about the difference between “ViewResult
” and “ActionResult
” and I hope that your confusion ends here.
So without wasting time, let’s get the difference in one sentence:
“ActionResult is an abstract parent class from which ViewResult class has been derived”.
So when you see MVC controller and action codes as shown below:
public ActionResult Index()
{
return View();
}
The above code means that you are returning a “ViewResult
” object and due to polymorphism, this object is automatically type casted to the parent class type, i.e., “ActionResult
”.
In case you are a newbie to polymorphism, let’s do a quick revision. In polymorphism, the parent class object can point towards any one of its child class objects at runtime. For example, in the below code snippet, we have parent “Customer
” class which is inherited by “GoldCustomer
” and “SilverCustomer
” class.
public class Customer
{}
public class GoldCustomer : Customer
{}
public class SilverCustomer : Customer
{}
So down the line, later during runtime, your parent “Customer
” object can point to any one of the child objects, i.e., “GoldCustomer
” or “SilverCustomer
”.
Customer obj = new Customer();
obj = new GoldCustomer();
obj = new SilverCustomer();
How does this polymorphism benefit for MVC results?
MVC applications are used to create web sites or web application. Web applications work in a request and response structure because they follow HTTP protocol.
So the end user using theUI sends a HTTP POST
or a GET
request and the web application depending on scenarios sends a response. Now the request is a quiet standard but the response types differ due to different kind of devices.
For example, if it's a browser you expect HTML response, if its jquery, you expect JSON format or for some other client types, you just want simple text contents and so on.
So what the MVC team did is they created a base general class called “ActionResult
” which was further inherited to create different types of results.
So in the action result code, you can pass some parameter from the UI and depending on this parameter, you can send different response types. For instance in the below example, we are passing a boolean flag whether it’s an HTML view or not and depending on the same, we are streaming different views.
public ActionResult DynamicView(bool IsHtmlView)
{
if (IsHtmlView)
return View();
else
return Json();
}
Cool, isn’t it, rather than writing different methods for each response, you just have one method with dynamic polymorphic response.
There are 11 different response types which can be sent to the end user:
ViewResult
- Renders a specified view to the response stream PartialViewResult
- Renders a specified partial view to the response stream EmptyResult
- An empty response is returned RedirectResult
- Performs an HTTP redirection to a specified URL RedirectToRouteResult
- Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data JsonResult
- Serializes a given object to JSON format JavaScriptResult
- Returns a piece of JavaScript code that can be executed on the client ContentResult
- Writes content to the response stream without requiring a view FileContentResult
- Returns a file to the client FileStreamResult
- Returns a file to the client, which is provided by a Stream FilePathResult
- Returns a file to the client
Are you struggling to learn ASP.NET MVC, start with the below video:
CodeProject
For further reading do watch the below interview preparation videos and step by step video series.