Action Method In ASP.NET MVC 5

Let us learn all about Action Method with examples in ASP.NET MVC 5 through this article.
 
 
In this article, you will learn the following points about the Action Method in MVC 5.
  • What is the Action Method in MVC 5?
  • Type of Action method
  • How to restrict the Action Method.
  • How to call Action Method?
  • How to change default Action Method?
  • How does the default action get executed?
Following are the previous articles in ASP.NET MVC 5 series.

What is the Action Method in MVC 5

 
ASP.NET MVC action methods are responsible to execute the request and generate a response to it. All the public methods of the MVC Controller are action methods. If we want the public method to be a  non-action method, then we can decorate the action method by “NonAction” attribute. It will restrict the action method to render on the browser.
 
To work with action method we need to remember the following points.
  • The action method is the public non-static method.
  • Action method can not be the private or protected method.
  • Action method can not contain ref and out parameters.
  • Action method can not be an extension method.
  • Action method can not be overloaded.
Example of an action method in ASP.NET MVC 5 is as below.
  1. public class HomeController : Controller  
  2.     {  
  3.         // GET: Home  
  4.         public ActionResult Index()  
  5.         {  
  6.             return View();  
  7.         }  
  8. }  
You can see in the above action method example that the Index() method is a public method which is inside the HomeController class. And the return type of the action method is ActionResult. It can return using the “View()” method. So, every public method inside the Controller is an action method in MVC.
 
Action Method can not be a private or protected method.
  1. public class HomeController : Controller  
  2.     {  
  3.         // GET: Home  
  4.         private ActionResult Index()  
  5.         {  
  6.             return "This is Index Method";          
  7.         }  
  8. }  
If you provide the private or protected access modifier to the action method, it will provide the error to the user, i.e., “resource can not be found” as below.
 
An action method cannot contain ref and out parameters. 
  1. public string Index(ref int id)  
  2.         {  
  3.             return "This is Index Method" +id;  
  4.         }  
  1. public string Index(out int id)  
  2.        {  
  3.            id = 0;  
  4.            return "This is Index Method" +id;  
  5.        }  
We can not provide the ref and out parameters to the action method. If you try to provide the ref and out parameters to the action method, then it will throw an error as below. 
 
 
Action method can not be overloaded.
  1. public string Index()  
  2.        {            
  3.            return "This is Index Method";  
  4.        }  
  5.   
  6.        public string Index(int id)  
  7.        {  
  8.            return "This is Index overloaded Method";  
  9.        }  
If you try to overload the action method, then it will throw an ambiguous error.
 
 
Types of Action Method.
  • Action Method
  • Non Action Method

How to restrict the public action method in MVC

 
To restrict the public action method in MVC, we can use the “NonAction” attribute. The “NonAction” attribute exists in the “System.Web.MVC” namespace.
 
Example of NonAction attribute in ASP.NET MVC.
  1. [NonAction]  
  2.   public string GetFullName(string strFirstName, string strLastName)  
  3.   {  
  4.           return strFirstName + " " + strLastName;  
  5.   }  

How to call Action Method?

 
Syntax
 
YourDomainName/ControllerName/ActionMethodName
 
Example
 
localhost/Home/Index
 
 

How to change default Action Method?

 
The default Action method is configured in the RouteConfig.cs class. By default, the Action Method is the “Index” action method. Change the “Index” action method name as per our requirement. 
  1. public static void RegisterRoutes(RouteCollection routes)  
  2.         {  
  3.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.   
  5.             routes.MapRoute(  
  6.                 name: "Default",  
  7.                 url: "{controller}/{action}/{id}",  
  8.                 defaults: new { controller = "Home", action = "Message", id = UrlParameter.Optional }  
  9.             );  
  10.         }  
You can see in the above code that we have changed the action method name to “Message” instead of “Index” by default.
 

How does the default action get executed?

 
Default controller and action method are configured in RouteConfig.cs class. By default, Controller is the Home Controller and default Action Method is the “Index” action method.
 
Example of default action method
  1. public static void RegisterRoutes(RouteCollection routes)  
  2.         {  
  3.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.   
  5.             routes.MapRoute(  
  6.                 name: "Default",  
  7.                 url: "{controller}/{action}/{id}",  
  8.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  9.             );  
  10.         }  
References
  • What is the Action Method in ASP.NET MVC/MVC 5?
  • How to restrict the Action Method.
  • How to call Action Method?
  • How to change default Action Method?
  • How does the default action get executed?
I hope you understand the concept of an action method in ASP.NET MVC 5. If you like my article, please add your valuable comment and share this article that will help to encourage me to create more articles.
 
Thanks for reading.


Similar Articles