ActionResult in MVC

ActionResult is a parent class that represents result of an action method. After execution of action method, it will return an object of ActionResult class or a child class derived from ActionResult class. In ASP.Net MVC, it is recommended to specify return type of every action method as ActionResult.

You can return any type of result such as View, File, Redirection, JSON etc from action method.In some cases we want to return response based on some conditions.
For example , in action method GetCustomer() , customeid is required as input. If customer record is found then that record is returned otherwise exception is returned specifying that customer record is not found. So we need to specify return type of GetCustomer() action method as ActionResult because exact return type is not predefined.
ActionResult is an abstract class that has many child classes such as ContentResult, ViewResult, FileResult, PartialViewResult, RedirectResult, RedirectToRouteResult, JsonResult etc. If we specify ActionResult as return type of an action method then we can return object of any child class. If we have specified return type of an action method as ViewResult then we must return View from that action method. In below code, we have specified return type of Index() action method as ContentResult , since ViewResult is child class of ContentResult class so we can return View from Index() action method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCApplication.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            return View();
        public ActionResult About()
            return View();
        public ActionResult Contact()
            return View();

Below is the details of different types of result classes for an action method:

1. ContentResult
It is generally used to return plain text from action method.We can return any content with specific content-type. For example, there is a customer object and we want to return Customer Name based on CustomerId, in that case return type of action method should be ContentResult because Customer Name is string type and we are returning string from action method.

2. ViewResult
ViewResult represents result of a view. We use ViewResult to send rendered html output of the view to browser. For example we want to display Registration page to user then we will create presentation logic in the view and return html output of View as ViewResult.

3. FileResult
We use FileResult to send content of the file to browser.For example, customer wants to get order details as pdf document. This pdf file will be represented as FileResult.

4. JsonResult
We can use JsonResult class to send an object that is converted as a JSON string.

5. RedirectResult
It represents redirection to other website.We use RedirectResult class for redirecting to specific URL which is outside of our website.For example, if user clicks on any social icon (e.g. facebook) then it will be redirected to that website.

6. RedirectToRouteResult
We use RedirectToRouteResult for redirecting to any specific action method within our web application. For example if user registration is successful then user will be redirected to a welcome page.

7. PartialViewResult
It represents the result of a partial view. We use PartialViewResult class to send content of a partial view to an action method. Action method should be child action method. We will learn about child action method in upcoming articles.

We will see practical implementation of these child classes to return different types of response from action methods in upcoming articles.

Leave a Comment

Your email address will not be published. Required fields are marked *