Monday, December 19, 2011

Differnce Between Actionresult and VewResult in ASP.NET MVC

ActionResult is an abstract class means ViewResult derives from ActionResult. You declare it this way so you can take advantage of polymorphism and return different types in the same method.

Eg:
public ActionResult Foo()
{
   if (someCondition)
     return View(); // returns ViewResult
   else
     return Json(); // returns JsonResult
}
I above example the return type of Foo() is "ActionResult" so now i am not bound to return a specific type of value, i can return a View/Json/PartialView/RedirectResult. If i would have used "ViewResult" then i would be able to return "View" only. So ActionResult provides us facility to take advantage of polimorphism in Type of return.

ActionResult 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

Compiled By: Rajesh Rolen

Read More
Powered By Blogger · Designed By Seo Blogger Templates