Action Method in MVC Framework

In this article, I am going to discuss the ideas about Action Method in MVC. Please read our previous articles before moving on to this article where we discussed Routing in ASP.NET MVC. As part of this article, we are going to discuss the following contents which are related to the Action Method in MVC.

All the public methods of the Controller class are called Action methods. The action Method in MVC is like any other normal method.

Action Method in MVC

Now You will discuss the action method of the controller.

  • Action method must be public.
  • It cannot be private or protected.
  • It cannot be overloaded and static method.
  • It cannot be an extension method.
  • It cannot have open generic types.
  • It contain ref or out parameters
  • It can process the logic and Error handling.
Action Method in MVC
Action Method in MVC

The Index() method is public, and it returns the ActionResult using the View() method. The View() method is defined in the Controller base class, which returns the appropriate ActionResult.

Default Action Method

Every MVC application has its own default action method as per the route configuration. The index is a default action method for any controller. Please see the below screenshot for understanding.

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 }
    );
}

Default Action Method configuration in routeconfig.cs file as you see in the above code. Action Method in MVC Framework

By default, the Index() method is a default action method for any controller, as per configured default root, as shown below. However, you can change the default action name as per your requirement in the RouteConfig class.

ActionResult Return Type

MVC framework has various types of action results available and those instance of a class derives from ActionResult. The ActionResult class is the base class for all the action results. There are various action result types available in MVC based on their task and performance. See most time MVC action method returns a View, which is derived from the ActionResult base class. Action Method in MVC Framework

Please follow the below table lists for all the action result return types.

ActionResultHelper MethodDescription
ViewResultView()Return as a web page
PartialViewResultPartialView()Renders the specified view to responding with an HTML fragment
RedirectResultRedirect()Redirects to another action method by using its URL
RedirectToRouteResultRedirectToAction() / RedirectToRoute()Redirects to another action method
ContentResultContent()Writes a string value directly into the response
JsonResultJson()Responds to the client with data in JavaScript Object Notation (JSON)
JavaScriptResultJavaScript()Responds to the client with a script for the client to execute
FileResultFile()Returns binary output to write to the response
EmptyResultNoneBlank HTTP response

Use different methods mentioned in the above table to return a different type of result from an action method.

Action Method Parameters

Every action method can have input parameters as normal methods. It can be primitive data type or complex type parameters, as shown below.

[HttpGet]
public ActionResult GetData()
{
    var empDetails = GetEmpolyeeDetails();
    return View(empDetails);
}
[HttpGet]
public ActionResult GetDataByID(int id)
{
    var empDetails = GetEmpolyeeDetailsByID(id);
    return View(empDetails);
}

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 or cookie 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 match.

Warm UP – Action Method in MVC Framework

  • Action method must be public.
  • Action method cannot be private or protected.
  • Action method cannot be overloaded and static method.
  • Action method cannot be an extension method.
  • Action method cannot have open generic types.
  • Action method contain ref or out parameters
  • Action method can process the logic and Error handling.

Please read more about the Action method in MVC from Microsoft.

Leave a Comment

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