We will learn Action Method in ASP.NET MVC 5 with example and example of Action Method in ASP.NET MVC 5 with example in this article. The default configuration of the action method in asp.net MVC 5.
Following are the previews article on ASP.NET MVC
Action Method in ASP.NET MVC 5:
ASP.NET MVC action methods are responsible to execute the request and generate a response on it. All the public method of MVC controller are action method. If we want the public method is 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 need to remember the following points.
- The action method is a public non-static method.
- Action method cannot be a private or protected method.
- Action method cannot contain ref and out parameters.
- Action method cannot be an extension method.
- Action method cannot be overloaded.
Example of an action method in ASP.NET MVC as follows.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
You can see in the above action method example, 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 “View()” method. So every public method inside the Controller is an action method in MVC.
Action method can not be private or protected method:
public class HomeController : Controller
{
// GET: Home
private string Index()
{
return "This is Index Method";
}
protected string Index1()
{
return "This is Index Method";
}
}
If you provide the private or protected access modifier in then it will provide the error to the user I.e. “resource can not found” as follows.
Action method can not contain ref and out parameters:
public class HomeController : Controller
{
public string Index(ref int id)
{
return "This is Index Method" + id;
}
}
public class HomeController : Controller
{
public string Index(out int id)
{
id = 0;
return "This is Index Method" + id;
}
}
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 follow.
Action method can not be overloaded:
public string Index()
{
return "This is Index Method";
}
public string Index(int i)
{
return "This is Index Method"+i;
}
If you try to overload the action method then it will throw an ambiguous error as follows.
Type of Action method
- Action Method
- Non Action Method
How to call Action Method:
Syntax: YourDomainName/ControllerName/ActionMethodName
Example: localhost/Home/Index
How to change default Action Method:
Default Action method is configured in RouteConfig.cs class. By default Action method is the “Index” action method. Change the “Index” action method name as per our requirement as bellow code.
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 = "Message", id = UrlParameter.Optional }
);
}
}
You can see the above code we have changed the action method name as “Message” instead of “Index” by default.
How to restrict the public action method in MVC:
To restrict the public action method in MVC. We can use “NonAction” attribute. The “NonAction” attribute exists in “System.Web.Mvc” namespace.
Example of NonAction attribute in ASP.NET MVC:
[NonAction]
public string GetFullName(string strFirstName, string strLastName)
{
return strFirstName + " " + strLastName;
}
Default Action Method in MVC:
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 as follows:
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 }
);
}
Interview questions for freshers on the Action Method,
- 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?
More detail watch follow video on Action Method in ASP.NET MVC 5:
ASP.NET MVC 5 Tutorials Step By Step:
References:
I hope you understand the concepts of an action method in asp.net MVC 5
Thanks for reading.
Nice Article.. good explanation..great work. thank for sharing.
ReplyDeleteThanks for reading.
ReplyDeleteStep by Step explanation really great article...
ReplyDeleteThankyou soo much. That was a great explaination. But still have a question. Why we can not use pass by reference. I want a technical answer please instead of error message screenshot. Is it a ajax request or anything else ?
ReplyDeleteGood question
ReplyDeleteFirst, we know the basic idea of ref and out variable. both the variable are used to store address of the value. So it is not possible to pass address of variable using URL or other way.
Thanks.