Click here to Skip to main content
13,630,992 members
Click here to Skip to main content
Add your own
alternative version

Stats

123.9K views
29 bookmarked
Posted 1 Nov 2014
Licenced CPOL

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

, 1 Nov 2014
Rate this:
Please Sign up or sign in to vote.
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”.

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

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.

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(); // 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:

License

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

Share

About the Author


You may also be interested in...

Pro

Comments and Discussions

 
QuestionAction Result Pin
Sandeep Gurjar15-May-18 0:02
memberSandeep Gurjar15-May-18 0:02 
QuestionAppreciated Brother Pin
Adalat Khan19-Feb-15 21:36
professionalAdalat Khan19-Feb-15 21:36 
GeneralQuick and good clarification Pin
Daniel B.1-Feb-15 2:31
memberDaniel B.1-Feb-15 2:31 
QuestionAwesome Article Pin
szeeshanali30-Jan-15 1:42
memberszeeshanali30-Jan-15 1:42 
QuestionNice job! Pin
Neil Diffenbaugh6-Nov-14 2:39
memberNeil Diffenbaugh6-Nov-14 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180712.1 | Last Updated 2 Nov 2014
Article Copyright 2014 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid