Action Methods:-

In ASP.NET MVC applications user interaction is organized around controllers and action methods. The controller defines action methods and each controller can include as many action methods as needed.

Action methods typically have a one-to-one mapping with user interactions. E.g user enters the URL in to the browser and submits the form. Request will be send to the server; based on routing logic, system will initialize the controller and call the corresponding action method.

Let’s take the simple controller class and understand more about the controller and action method

Case 1:

Url: http://localhost:12744/Home/Contact

Format: http://[ServerName]:[Port]/[controllerName]/[actionName]

  • In the above url ‘Home/Contact’ is called as sub-path of the url
  • Sub-path will mention about the controller name and its action method to be called
  • Routing engine treat Home as controller which is prefixed with controller (e.g HomeController) and Contact as action method inside the controller class.
  • Routing engine call the Contact action method and return response back to the browser

Case 2:

Url: http://localhost:12744/Home

Format: http://[serverName]:[port]/[controllerName]

  • Routing logic treat ‘Home’ as controller name
  • Action Method – Index [If user does not mention the action method name in the url ‘Index’ is consider as default action method to be called]

Case 3:

Url: http://localhost:12744/Home/Details/5

Format: http://[serverName]:[port]/[controllerName]/[parameter]

  • Routing logic treat ‘Home’ as controller name
  • ‘Details’ method inside the Home controller is consider as action method to process the request
  • ‘5’ will be send as parameter value to the ‘Details’ action method