Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Difference Between ViewResult() and ActionResult() in MVC?

4.69/5 (37 votes)
1 Nov 2014CPOL3 min read 161.2K  
In this article, I have explained the difference between ViewResult() and ActionResult() in MVC.

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

Image 1

So when you see MVC controller and action codes as shown below:

”html”
public ActionResult Index()
{
      return View(); // this is a view result class
}

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.

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

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

Image 2

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.

Image 3

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.

Image 4

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.

”html”
public ActionResult DynamicView(bool IsHtmlView)
{
   if (IsHtmlView)
     return View(); // returns simple ViewResult
   else
     return Json(); // returns JsonResult view
}

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:

  1. ViewResult - Renders a specified view to the response stream
  2. PartialViewResult - Renders a specified partial view to the response stream
  3. EmptyResult - An empty response is returned
  4. RedirectResult - Performs an HTTP redirection to a specified URL
  5. RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
  6. JsonResult - Serializes a given object to JSON format
  7. JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
  8. ContentResult - Writes content to the response stream without requiring a view
  9. FileContentResult - Returns a file to the client
  10. FileStreamResult - Returns a file to the client, which is provided by a Stream
  11. FilePathResult - Returns a file to the client

Are you struggling to learn ASP.NET MVC, start with the below video:

Image 5

For further reading do watch the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)