6
What’s the difference between Viewresult and Actionresult? I usually call a View using an Actionresult, but I’ve seen code with Viewresult. Then this doubt arose.
6
What’s the difference between Viewresult and Actionresult? I usually call a View using an Actionresult, but I’ve seen code with Viewresult. Then this doubt arose.
7
ViewResult
drift ActionResult
. ActionResult
is abstract, and serves as wildcard in return, which can be:
ViewResult
: Specifically returns a View;PartialViewResult
: Specifically returns a Partial View;EmptyResult
: Returns an empty answer;RedirectResult
: Returns a redirect instruction to another address, not necessarily from your system;RedirectToRouteResult
: Returns a specific redirect instruction to another route-based address of your system;JsonResult
: Returns a JSON;JavaScriptResult
: Returns a server generated Javascript code to be executed in client;ContentResult
: Returns a custom result, such as a string, for example;FileContentResult
: Returns a file. Equivalent to FileResult
;FileStreamResult
: Returns a file whose reading is based on an object of type Stream
or derivatives.FilePathResult
: Returns a file based on a directory path of the server file system that hosts your system.4
Of a response from forum of ASP.NET: ActionResult
is an abstract class, and has derived classes, including ViewResult
. If you will return in your method a Viewresult, you can declare it either by returning Actionresult or Viewresult.
These are some classes derived from Actionresult:
Who voted negative? The answer is correct.
Carlos, the code that gave rise to my doubt is this one below. Within the method itself were used two types of calling a View. The First using Actionresult and the second calling its derived class which is also abstract, the Viewresult: public Actionresult Change(Product product) { Return View(product); } public Viewresult New() { Return View("Change", new Product(); }
Pro ASP.NET, a action need to return a ActionResult
, or one of its derived classes - in your case, it will make no difference. An advantage of declaring the method by returning the concrete type (for example, ViewResult
), is that it prevents that in the future someone will change the type of return by mistake (and that’s what the compiler will focus on, not ASP.NET).
Thanks Carlos for the help!
Browser other questions tagged asp.net-mvc-5
You are not signed in. Login or sign up in order to post.
I liked the Gypsy explanations. I was looking at the Viewresult documentation and realized that it is also an abstract class the doubt arose because I saw a controller method using Actionresult and another controller method called a View using Viewresult instead of Actionresult. If I understand your explanations correctly, it doesn’t matter if I call a View with Actionresult or Viewresult?
– Kelly Soares
Exactly,
ActionResult
, by being more generic, supports not onlyreturn View()
as well as any other return as those mentioned in the reply.ViewResult
will therefore only acceptreturn View()
or some method that returns onlyViewResult
, what may turn out to be a limiter.– Leonel Sanches da Silva
Thank you for the reply!
– Kelly Soares