Hello readers In this tutorial we are going to see what is the difference between ViewResult() and ActionResult() in ASP.NET MVC. This is a very interesting topic for interview point of view.

Difference between ViewResult() and ActionResult() in ASP.NET MVC:

 

 

 

View Result

ActionResult

1- ViewResult can return only view result type. Action Result can return many types of results like PartialViewResult, JSONResult.
2- ViewResult class is derived from the base class ActionResult. ActionResult is a base class for ViewResult.
3- It is a concrete class. It is an abstract class.
4- It is a concrete class so no subtypes available.

ActionResult has many subtypes available:

  • ViewResult – Renders a specified view to the response stream.
  • PartialViewResult – Renders a specified partial view to the response stream.
  • EmptyResult – An empty response is returned.
  • RedirectResult – Performs an HTTP redirection to a specified URL.
  • RedirectToRouteResult – Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data.
  • JsonResult – Serializes a given object to JSON format.
  • JavaScriptResult – Returns a piece of JavaScript code that can be executed on the client.
  • ContentResult – Writes content to the response stream without requiring a view.
  • FileContentResult – Returns a file to the client.
  • FileStreamResult – Returns a file to the client, which is provided by a Stream.
  • FilePathResult – Returns a file to the client.
5-

If we are sure that our action method will return the only view we can go with ViewResult.

For Example:
public ViewResult Index()
{
return View(); // this is a view result class
}

If our action method may return view or redirect to some URL we should go with ActionResult.

For Example:

public ActionResult DynamicView(bool IsHtmlView)
{
if (IsHtmlView)
return View(); // returns simple ViewResult
else
return Json(); // returns JsonResult view
}

View More:

Conclusion:

I hope you understand the Difference between VieweResult() and ActionResult() in ASP.NET MVC. Your Feedback and comments are welcome to me.

Thank You,

Leave a Comment