ActionResult is an abstract class, and it's base class for ViewResult class.
In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it.
And ViewResult is an implementation for this abstract class. It will try to find a view page (usually aspx page) in some predefined paths(/views/controllername/, /views/shared/, etc) by the given view name.
It's usually a good practice to have your method return a more specific class. So 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.
KentZhou
Participant
1018 Points
573 Posts
What's the difference between ActionResult and ViewResult for action method?
Jul 17, 2009 01:46 PM|LINK
When defining an action method in control, there is possible two signature:
public ActionResult MyAction(){ ...}
public ViewResult MyAction(){...}
What's the difference between ActionResult and ViewResult for action method?
nmendes
Member
36 Points
10 Posts
Re: What's the difference between ActionResult and ViewResult for action method?
Jul 17, 2009 02:29 PM|LINK
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
rxwen
Member
512 Points
71 Posts
Re: What's the difference between ActionResult and ViewResult for action method?
Jul 17, 2009 02:43 PM|LINK
ActionResult is an abstract class, and it's base class for ViewResult class.
In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it.
And ViewResult is an implementation for this abstract class. It will try to find a view page (usually aspx page) in some predefined paths(/views/controllername/, /views/shared/, etc) by the given view name.
It's usually a good practice to have your method return a more specific class. So 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.