Difference between Action result and View result

ActionResultis an abstract class while ViewResultderives from theActionResultclass.ActionResulthas several derived classes likeViewResult,JsonResult,FileStreamResult, and so on.

  • ActionResultcan be used to exploit polymorphism and dynamism. So if you are returning different types of views dynamically,ActionResultis the best thing. For example in the below code snippet, you can see we have a simple action calledDynamicView. Depending on the flag (IsHtmlView) it will either return aViewResultorJsonResult.
public  ActionResult DynamicView()
{   
if (IsHtmlView)     
return View(); 
//
 returns simple ViewResult
else
return Json(); 
//
 returns JsonResult view

}

results matching ""

    No results matching ""