Result Types in ASP.NET MVC

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

What is ASP.NET MVC's Action Method?

 In ASP.NET MVC, Action Methods are public Controller methods that respond to particular URLs. 

Rules for Creating an Action Method

When creating an Action Method, you must follow the following rules:

  • Public access is required to the action method.
  • It can't be overloaded.
  • It cannot remain static.

What is an Action Result in ASP.NET MVC?

The ActionResult is an abstract class that is the return type for action methods. It serves as the basis class for all outcomes an action method can provide. Action Results are classified into various derived types: ViewResult, JsonResult, and ContentResult. They specify how the output of an action method is presented or processed.


ActionResult Class

The ActionResult class is abstract, including a constructor and a function called ExecuteResult. This method handles the result of an action method and accepts a ControllerContext parameter containing information about the controller, HTTP content, request context, and route data.

Why is ActionResult in ASP.NET MVC an abstract class?

The ActionResult class is abstract to enable the return of various outcomes depending on business requirements. This abstraction ensures that the ASP.NET MVC framework can easily handle multiple return types produced from the ActionResult class. When you set the return type to ActionResult, the action method can return any type derived from this abstract class.

Types of Action Results in ASP.NET MVC

  • ViewResult: Returns a ViewResult that renders the provided or default view via the controller View() helper method.
  • PartialViewResult: Returns a PartialViewResult that renders the provided or default partial view (a view without a layout) using the controller's PartialView() helper method.
  • RedirectResult: Returns a RedirectResult that makes an HTTP 301 or 302 redirection to a given URL using the controller's Redirect() helper method.
  • RedirectToRouteResult: Returns a RedirectToRouteResult that redirects an HTTP 301 or 302 request to an action method or a specified route entry using the controller's RedirectToAction(), RedirectToActionPermanent(), RedirectToRoute(), and RedirectToRoutePermanent() helper methods.
  • ContentResult: Returns a ContentResult that renders raw text such as "Hello, DotNet Tricks!" using the controller Content() helper method.
  • JsonResult: Returns a JsonResult that serializes an object in JSON format (e.g. {"Message": "Hello, World!"}) and renders it using the controller's Json() helper method.
  • JavaScriptResult: Using the controller JavaScript() helper method, a JavaScriptResult is returned that contains a sample of JavaScript code, such as "function hello() { alert('Hello, World!'); }". This is solely used for AJAX circumstances.
  • FileResult: Returns a FileResult that renders the contents of a file, such as PDF, DOC, or Excel, using the controller's File() helper method.
  • EmptyResult: Returns no result from an action. This does not have any controller helper methods.
  • HttpNotFoundResult: Returns a HttpNotFoundResult that generates a 404 HTTP Status Code response using the controller's HttpNotFound() helper method.
  • HttpUnauthorizedResult: Returns a HttpUnauthorizedResult with a 401 HTTP Status Code ("not authorized") answer. This does not have any controller helper methods. This is used for authentication (forms authentication or Windows authentication) and prompts the user to log in.
  • HttpStatusCodeResult: Returns a HttpStatusCodeResult that displays the supplied HTTP code response. This does not have any controller helper methods.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this