Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Monday, May 6, 2019

Action Method In ASP.NET MVC 5


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.

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.

Action Method in ASP.NET MVC 5




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 In  MVC 5
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 In MVC 5

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.

Action Method In MVC 5


Type of Action method
  • Action Method
  • Non Action Method

How to call Action Method:

Syntax: YourDomainName/ControllerName/ActionMethodName

Example: localhost/Home/Index
Action Method In MVC 5
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.Mvcnamespace. 

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,

  1. What is the Action Method in ASP.NET MVC/MVC 5?
  2. How to restrict the Action Method.
  3. How to call Action Method?
  4. How to change default Action Method?
  5. 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.
Share:

5 comments:

  1. Nice Article.. good explanation..great work. thank for sharing.

    ReplyDelete
  2. Step by Step explanation really great article...

    ReplyDelete
  3. Thankyou 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 ?

    ReplyDelete
  4. Good question
    First, 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.

    ReplyDelete

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave