ViewResult V/S ActionResult

2020-07-14 09:24发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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.