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 functions for the same functionality. Is there something that sets them apart?

+4
source share
3 answers

ActionResultis the base type, while it ViewResultis a subtype ActionResult.

When you set the type of the returned action ActionResult, you can return any subtype of it, for example, Json, PartialView, View, RedirectToAction .

, ViewResult, , , .

ActionResult , Action ( , ActionResult), forums.asp.net

ActionResult - , ( "ASP.NET MVC 1.0 Quickly):

     

a) ViewResult

     

     

b) PartialViewResult

     

     

c) EmptyResult

     

;

     

d) RedirectResult

     

HTTP URL

     

e) RedirectToRouteResult

     

HTTP URL-,

     

f) JsonResult

     

ViewData JSON

     

g) JavaScriptResult

     

JavaScript,   

     

h) ContentResult

     

     

i) FileContentResult

     

     

j) FileStreamResult

     

, Stream

     

k) FilePathResult

     

ViewResult

, , ViewResult. , , . ActionResult.

:

http://forums.asp.net/t/1448398.aspx

:

http://www.slideshare.net/umarali1981/difference-between-action-result-and-viewresult

+16

, ,

public ViewResult MyView ()
{
   return View();
}

, ,

public ActionResult MyView ()
{
   //can return view
   //can return partial view
   //can return json
   //can return javascript
   // redirect to another action
   // and many more  :)
}

OOPS, , ActionResult, .

ActionResult, ,

0

The first thing to understand --- ActionResult and ViewResult is basically a return type for a method (for example, an action in MVC).

The second difference --- ActionResult can return any type of result, if ViewResult can return the type of result View only.

0
source

Source: https://habr.com/ru/post/1547368/


All Articles