top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between ActionResult and ViewResult?

+3 votes
294 views
What is the difference between ActionResult and ViewResult?
posted Jul 6, 2015 by Shivaranjini

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

ActionResult" is an abstract class while "ViewResult" derives from "ActionResult" class. "ActionResult" has several derived classes like "ViewResult" ,"JsonResult" , "FileStreamResult" and so on.

"ActionResult" can be used to exploit polymorphism and dynamism. So if you are returning different types of view dynamically "ActionResult" is the best thing. For example in the below code snippet you can see we have a simple action called as "DynamicView". Depending on the flag ("IsHtmlView") it will either return "ViewResult" or "JsonResult".

public ActionResult DynamicView()
{
if(IsHtmlView)
    return View()://return simple ViewResult
else
return Json(); // returns Json Result View
}
answer Jul 7, 2015 by Manikandan J
...