8

How to decide whether to use ActionResult or ViewResult? I know that ActionResult is an abstract class with ViewResult as a subtype but I have seen examples using both of these for the same functionality. Is there something that differentiates between them?

tereško
  • 58,060
  • 25
  • 98
  • 150
Mani
  • 379
  • 1
  • 4
  • 22
  • 1
    possible duplicate of [Difference Between ViewResult() and ActionResult()](http://stackoverflow.com/questions/4743741/difference-between-viewresult-and-actionresult) – whoah Jul 07 '14 at 06:27
  • Hi, new to programming here, why constrict, why not just use actionresult instead of viewresult? is it just a security reason, program leakage reason? –  Mar 28 '18 at 17:15

3 Answers3

20

ActionResult is base type while ViewResult is subtype of ActionResult.

When you set Action's return type ActionResult, you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

But when you use subtype as in this case ViewResult you are bounding your action that it will only return subtype as result which in this case is View.

When you use ActionResult as return type, you can return the following from Action(which means following are the subtypes of ActionResult), as explained on forums.asp.net:

ActionResult is a general result type that can have several subtypes (from "ASP.NET MVC 1.0 Quickly" Book):

a) ViewResult

Renders a specifed view to the response stream

b) PartialViewResult

Renders a specifed partial view to the response stream

c) EmptyResult

Basically does nothing; an empty response is given

d) RedirectResult

Performs an HTTP redirection to a specifed URL

e) RedirectToRouteResult

Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data

f) JsonResult

Serializes a given ViewData object to JSON format

g) JavaScriptResult

Returns a piece of JavaScript code that can be executed on the client

h) ContentResult

Writes content to the response stream without requiring a view

i) FileContentResult

Returns a fle to the client

j) FileStreamResult

Returns a fle to the client, which is provided by a Stream

k) FilePathResult

Returns a fle to the client

When to Use ViewResult

if you are sure that your action method will return some view page, you can use ViewResult. But if your action method may have different behavior, like either render a view or perform a redirection. You can use the more general base class ActionResult as the return type.

For more details:

http://forums.asp.net/t/1448398.aspx

Also you can refer here:

http://www.slideshare.net/umarali1981/difference-between-action-result-and-viewresult

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Didn't realize there were so many return types ! Jazakallah, As of now in asp.net mvc 5, the more complete list goes here in [MSDN - ActionResult Class](https://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx) – Irf Jan 12 '17 at 06:14
  • Hi, new to programming here, why constrict, why not just use actionresult instead of viewresult? is it just a security reason, program leakage reason? –  Mar 28 '18 at 17:14
  • there is no harm in using `ActionResult` too, you can – Ehsan Sajjad Mar 28 '18 at 17:15
0

If you use View result you have to return a view like

public ViewResult MyView ()
{
   return View();
}

If you mark it to Action result you can return variety of result like

public ActionResult MyView ()
{
   //can return view
   //can return partial view
   //can return json
   //can return javascript
   // redirect to another action
   // and many more  :)
}

And as the basic of OOPS that always code for abstraction rather then concrete ActionResult is always a plus to use.

For more information on ActionResult please follow the link

Ashish Rajput
  • 1,489
  • 1
  • 11
  • 20
0

First to Understand---ActionResult and ViewResult are basically a return type for an method(i.e Action in MVC).

Second The Difference is --- ActionResult can return any type of Result Whereas ViewResult can return result type of View Only.