Difference Between Viewresult() and ActionResult()

ActionResult is an abstract class that can have several subtypes:

a) ViewResult - Renders a specifed view to the response stream

b) PartialViewResult - Renders a specifed partial view to the response stream

c) EmptyResult - An empty response is returned

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

You should use ViewResult to make code more readable and find bugs easier, but there can be other benefits too.

Since I use POST-REDIRECT-GET pattern, I wrote some tests to check if every [HttpPost] method returns RedirectToRouteResult. If I define other type of result in [HttpPost] method, it automatically doesn't pass test. This prevents me from returning View(model) in post method.

It's usually a good practice to have your method return a more specific class. So 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.

Comments

Anonymous said…
thanks for sharing about difference between action result and view result your concepts really very informative to us. keep sharing.
Dotnet Training in Chennai

Popular posts from this blog

Interview Questions to Ask the Employer

Place .NET DLL in GAC

Windows Communication Foundation - FAQ