Difference between ActionResult() and ViewResult()

2
S.No ActionResult() ViewResult() 1 What is ActionResult() ? ActionResult() is a general result type that can have several subtypes. ActionResult() is an abstract class. ActionResult() is a base class for ViewResult() In MVC framework, it uses ActionResult class to reference the object our action method returns. And invokes ExecuteResult method on it. What is ViewResult() ? ViewResult() renders a specifed view to the response stream. ViewResult() is a concrete class. ViewResult() is a derived class of ActionResult() ViewResult is an implementation for this abstract class (ActionResult 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. 2 What are the subtypes of ActionResult class? . 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 What are the subtypes of ViewResult class? As it is a concrete class, so subtypes are not available for ViewResult class.

description

Difference between ActionResult() and ViewResult()

Transcript of Difference between ActionResult() and ViewResult()

Page 1: Difference between ActionResult() and ViewResult()

S.No ActionResult() ViewResult()

1 What is ActionResult() ?ActionResult() is a general result type that can have several subtypes.

ActionResult() is an abstract class.

ActionResult() is a base class for ViewResult()

In MVC framework, it uses ActionResult class to reference the object our action method returns. And invokes ExecuteResult method on it.

What is ViewResult() ?ViewResult() renders a specifed view to the response stream. ViewResult() is a concrete class.

ViewResult() is a derived class of ActionResult()

ViewResult is an implementation for this abstract class (ActionResult 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.

2 What are the subtypes of ActionResult class?

. 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 HTTPredirection to a specifed URL

• RedirectToRouteResult - Performs an HTTP redirection to a URL that isdetermined 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

What are the subtypes of ViewResult class?As it is a concrete class, so subtypes are not available for ViewResult class.

Page 2: Difference between ActionResult() and ViewResult()

• FilePathResult - Returns a file to the client

3 Example:

(OR)

"ActionResult" can be used to exploit polymorphism and dynamism. So if we are returning different types of view dynamically "ActionResult" is the best thing. For example in the above code snippet we can see we have a simple action called as "DynamicView". Depending on the flag ("IsHtmlView") it will either return "ViewResult" or "JsonResult".

Example:

4 When to go for ActionResult class ?If our action method may have different behavior, like either render a view or perform a redirection. We can use the more general base class ActionResult as the returntype.

When to go for ViewResult class ?If we are sure that our action method will return some view page, we can use ViewResult.

Reference:

http://onlydifferencefaqs.blogspot.in/2014/03/difference-between-actionresult-and.html