w3resource

Action method


In the previous tutorial, we learnt about the controller, in this tutorial, we will take a deeper look at the controller by looking at the action method of the controller class. These methods are like the regular methods with just a few restrictions:

  • Action method must be public. It can neither be private nor protected
  • Action method cannot be overloaded
  • Action method cannot be a declared as static.

The following is an example of Index action method of ActivitiesController

public ActionResult Index()
{
    return View();
}

From the above code snippet, the Index method was declared as a public method and it in turn returns an ActionResult using the View() method. The controller base class has the view method defined in it with a couple of other methods.

Default Method: The index method of every controller is configured as the default action method. This behavior can however be changed in the RouteConfig class of the RouterConfig.cs file in the App_Start folder.

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home",
   				   action = "Index", 
    				   id = UrlParameter.Optional
 				  }
            );
        }
    }

ActionResult

The ActionResult is the base class for the various result classes. ASP.NET MVC has a lot of other result classes. The table below enumerates some of these return classes and the type of data they return.

Result Class Description
ViewResult Represents HTML and markup
EmptyResult Represents No response
ContentResult Represents string literal
FileContentResult/FilePathResult/ FileStreamResult Represents the content of a file
JavaScriptResult Represent a JavaScript script
JsonResult Represent JSON that can be used in AJAX
RedirectResult Represents a redirection to a new URL
RedirectToRouteResult Represent another action of same or other controller
PartialViewResult Returns HTML from Partial view
HttpUnauthorizedResult Returns HTTP 403 status

All the above classes have the ActionResult as their base class, hence the ActionResult class can return any of the datatype which any of those classes returns. The type of class to choose depends on the type of data you intend to return.

The Index() method of ActivitesController in the above figure uses View() method to return ViewResult (which has ActionResultas a base class). The View() method is defined in base Controller class. It also contains different methods, which automatically returns particular type of result as shown in the below table.

Result Class Description Base Controller Method
ViewResult Represents HTML and markup. View()
EmptyResult Represents No response
ContentResult Represents string literal. Content()
FileContentResult, FilePathResult, FileStreamResult Represents the content of a file File()
JavaScriptResult Represent a JavaScript script JavaScript()
JsonResult Represent JSON that can be used in AJAX Json()
RedirectResult Represents a redirection to a new URL Redirect()
RedirectToRouteResult Represent another action of same or other controller RedirectToRoute()
PartialViewResult Returns HTML PartialView()
HttpUnauthorizedResult Returns HTTP 403 status

As you can see in the above table, View method returns ViewResult, Content method returns string, File method returns content of a file and so on. Use different methods mentioned in the above table, to return different types of results from an action method.

Action Method Parameters

Every action methods can have input parameters as normal methods. It can be primitive data type or complex type parameters as shown in the below example.

[HttpPost]
public ActionResult Edit(Activities id)
{
    return RedirectToAction("Index");
}

[HttpDelete]
public ActionResult Delete(int id)
{
    return RedirectToAction("Index");
}

By default, the values for action method parameters are retrieved from the request's data collection. The data collection includes name/values pairs for form data or query string values. Model binding in ASP.NET MVC automatically maps the URL query string or form data collection to the action method parameters if both names are matching



Inviting useful, relevant, well-written and unique guest posts