Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewResult V/S ActionResult

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?

like image 574
Mani Avatar asked Jul 07 '14 06:07

Mani


People also ask

Can I return ActionResult instead of ViewResult?

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.

What is ViewResult?

ViewResult represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object. View() creates an object that renders a view to the response. Step 8.

What is the difference between ActionResult and JsonResult?

Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client). Use ActionResult if you want to return a view, redirect etc to be handled by a browser.

What does ActionResult mean?

What is an ActionResult? An ActionResult is a return type of a controller method, also called an action method, and serves as the base class for *Result classes. Action methods return models to views, file streams, redirect to other controllers, or whatever is necessary for the task at hand.


1 Answers

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

like image 80
Ehsan Sajjad Avatar answered Oct 12 '22 02:10

Ehsan Sajjad