Difference Between ViewResult() and ActionResult()

ActionResult is an abstract class. ViewResult derives from ActionResult.  ViewResult is a subclass of ActionResult. The View method returns a ViewResult. So really these two code snippets do the exact same thing. The only difference is that with the ActionResult one, your controller isn’t promising to return a view – you could change the method body to conditionally return a RedirectResult or something else without changing the method definition.

You declare it this way so you can take advantage of polymorphism and return different types in the same method.

e.g:

public ActionResult Foo()
{
   if (someCondition)
     return View(); // returns ViewResult
   else
     return Json(); // returns JsonResult
}


Other derived classes include

ActionResult Subtypes

  • ViewResult – Renders a specifed view to the response stream
  • PartialViewResult – Renders a specifed partial view to the response stream
  • EmptyResult – An empty response is returned
  • RedirectResult – Performs an HTTP redirection to a specifed 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 ViewData 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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s