ASP.NET MVC

What is the Action Result in ASP.NET MVC?

What is the Action Result in ASP.NET MVC?

Action Result in ASP.NET MVC

Action Result is the return type of an action method. The action result is an abstract class, with one constructor and one method. It is the base class for all types that an action method returns
The constructor is basically used to initializes a new instance of the ActionResult class.
The ExecuteResult method enables the processing of the result of an action method by a custom type that inherits from the ActionResult class. The ExecuteResult method takes one parameter i.e. context in which the result is executed. The context includes the information of Controller, HTTP Content, request context, and route data.



Types of Action Results
There are many different types of Action Results that an action method can return in ASP.NET MVC. Each Action Result returns a different type of result format. ActionResult is the base class of all the result types. These results are categorized into three sections:


1. Content-Returning Action Result in ASP.NET MVC:

The Content-Returning ActionResults in ASP.NET MVC are responsible for returning content to the browser or calling the script.
ViewResult-  Represents HTML and markup. Return a ViewResult which renders the specified or default view by using controllers View() Helper Method. ViewResult is indirectly derived from the ActionResult abstract class. And we already know that ActionResult is the base class of different action results.

public class HomeController : Controller {
    public ViewResult Index() {
        return View();
    }
}

Since ASP.NET MVC follows the convention-over-configuration approach, MVC will look for a View named “Index” in the Views/Home subfolder, and then look in the Views/Shared subfolder and if it doesn’t find it then it will throw an InvalidOperationException.

What if I wanted to return a view other than the one that matches the action name? Then we need to explicitly specify the View name as shown below.

public class HomeController : Controller {
    public ViewResult Index() {
        return View("About");
    }
}

Now it will attempt to find a view with the name “About” in the Views/Home folder and if it is not found there, then it will search the Views/Shared subfolder.

PartialViewResult-  Represents HTML and markup. Return the PartialViewReslt which renders the specified or default partial view (a view without its layout), by using Controllers PartialView() Helper Method.

FileResult- Returns a FileReslt which renders the contents of a file like PDF, DOC, Excel by using Controllers File() Helper Method.
    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).    

ContentResult- Represents a Raw Text result, by using Controllers Content() Helper Method.

EmptyResult- Represents no result.

JsonResult- Represents a JavaScript Object Notation result / Json Script that can be used in an AJAX application as a snippet of JavaScript by using Controllers Json() Helper Method.

JavaScriptResult- Represents a JavaScript script, by using Controllers JavaScript() Helper Method



2. Redirection Action Result in ASP.NET MVC:

The Redirection ActionResults in ASP.NET MVC are responsible for redirecting to other URLs or actions.:
RedirectResult- Represents an Http 301 or 302 redirection to a new URL, by using Controllers Redirect() Helper Method.
RedirectToRouteResult- Represents an Http 301 or 302 redirection to a specific Route by using Controllers RedirectToRoute() Method or RedirectToRoutePermanenet() Helper Method.
RedirectToActionResult- Represents an Http 301 or 302 redirection to an action method by using Controllers RedirectToAction() Method or RedirectToActionPermanenet() Helper Method.

return RedirectToAction("Index");



3. Status Action Result in ASP.NET MVC:

The Status ActionResults in ASP.NET MVC are responsible for returning status codes to the browser.
HttpStatusCodeResult- Return a specified Http Code re sponse. This has no controller helper method.
HttpUnauthorizedResult- Return an HttpUnauthorizedResult which renders 401 Http Status Code (Not Authorized) response. This has no controller Helper method and used for Authentication (Form/ Window Auth).
HttpNotFoundResult- .Returns a HttpNotFoundResult which renders 404 Http Status Code response by using Controllers HttpNotFound() Helper Method.







Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram