ViewResult V/S ActionResult

asp.net-mvc, asp.net-mvc-3

Solution

`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

Problem

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?

Original source

Related problems