Home > Back-end >  Difference between ActionResult classes
Difference between ActionResult classes

Time:08-26

What is the difference between Microsoft.AspNetCore.Mvc.ActionResult and System.Web.Mvc.ActionResult ?

When should you use one or the other one?

CodePudding user response:

Microsoft.AspNetCore.Mvc.ActionResult

as it is showing, it is applied in asp.net core. this executes the result operation method asynchronusly. it is the default implementation of IActionResult.

System.Web.Mvc.ActionResult It is applied to ASP.NET MVC. it simply represents the result of an action method. It performs the operation synchronously.

CodePudding user response:

The System.Web.Mvc namespace is used the .NET Framework. If your project is targetting the .NET Framework, use System.Web.Mvc.ActionResult.

Below list is all return type from System.Web.Mvc.ActionResult :

  • ViewResult : Represents HTML and markup.
  • EmptyResult : Represents no result.
  • RedirectResult : Represents a redirection to a new URL.
  • JsonResult : Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • JavaScriptResult : Represents a JavaScript script.
  • ContentResult : Represents a plain text result.
  • FileContentResult : Represents a downloadable file (with the binary content).
  • FilePathResult : Represents a downloadable file (with a path).
  • FileStreamResult : Represents a downloadable file (with a file stream).

The Microsoft.AspNetCore.Mvc namespace is used in .NET Core. If your project is targetting .NET Core, use Microsoft.AspNetCore.Mvc.ActionResult.

ASP.NET Core offers the following options for web API controller action return types:

  • Specific type
  • IActionResult
  • ActionResult<T>

List of common return types in Microsoft.AspNetCore.Mvc.ActionResult :

  • ViewResult : Represents HTML and markup.

  • ContentResult : Represents content like plain text, XML, html.

  • JsonResult : Represents a JavaScript Object Notation result that can be used in an AJAX application.

  • PartialViewResult : Represents PartialView.

  • RedirectResult : Represents a redirection to a new URL.

  • RedirectToActionResult : Represents a redirection to specified action and controller.

  • RedirectToPageResult : Represents a redirection to a specified pageName (It is mostly used in Razor pages).

  • RedirectToRouteResult : Represents a route to redirect from one controller to another controller in cases we have more than one route.

  • StatusCodeResult : Represents an HTTP status code.

  • ViewComponentResult : Represents a "ViewComponent".

  • ChallengeResult : on execution invokes HttpContext.ChallengeAsync.

  • SignInResult : on execution invokes HttpContext.SignInAsync

  • SignOutResult : on execution invokes HttpContext.SignOutAsync.

  • Related