Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between ViewResult() and ActionResult()

What is the difference between ViewResult() and ActionResult() in ASP.NET MVC?

public ViewResult Index() {     return View(); }  public ActionResult Index() {     return View(); } 
like image 550
Domnic Avatar asked Jan 20 '11 05:01

Domnic


People also ask

Can I return ActionResult instead of ViewResult?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

What is the difference between JsonResult and ActionResult?

Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client). Use ActionResult if you want to return a view, redirect etc to be handled by a browser.

What do you mean by ViewResult ()?

ViewResult represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object. View() creates an object that renders a view to the response.

What is ActionResult used for?

The ActionResult method works as a return type of any controller method in the MVC. It acts as the base class of Result classes. It is used to return the models to the Views, file streams, and also redirect to the controllers. It is the responsibility of the Controller that connects the component.


1 Answers

ActionResult is an abstract class that can have several subtypes.

ActionResult Subtypes

  • ViewResult - Renders a specifed view to the response stream

  • PartialViewResult - Renders a specifed partial view to the response stream

  • EmptyResult - An empty response is returned

  • RedirectResult - Performs an HTTP redirection to a specifed 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 ViewData 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

Resources

  • What's the difference between ActionResult and ViewResult for action method? [ASP.NET Forums]
like image 105
Divi Avatar answered Oct 06 '22 03:10

Divi