Why action method must be public and it cannot be static any Reason???
Loading
Why action method must be public and it cannot be static any Reason???
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Oct 9, 2022, 6:01 AM
I assume it must be public so that it can be found (seen) from outside the class; in other words, ASP.Net must be able to see it. Making something (such as a method) private means that it is for use privately; only by the class.
Tuhin PaulPosted Mar 16, 2023, 9:47 AM
Making a method public allows it to be accessed and used from outside the class, so that other parts of the code or applications, such as ASP.Net, can use it. On the other hand, making a method private restricts its use to only within the class, allowing it to be used only for internal implementation and not accessible from outside.
Rajeev KumarPosted Mar 16, 2023, 7:41 AM
The action method must be public. It cannot be private or protected. Action method cannot be overloaded. Action method cannot be a static method
Uday DodiyaPosted Oct 8, 2022, 4:17 AM
If "Action method" is a method that returns ActionResult, you can declare it static, of course, but it will not be called when you initiate that action.
When calling some action, ASP.NET MVC creates an instance of the controller (using ControllerFactory), and since your method is static, it is not related to any instance, and thus, it will not be called by MVC.
The controller then determines the appropriate action method to handle the request. As per MSDN, a method must meet the following requirements:
The method must be public.
The method cannot be a static method.
The method cannot be an extension method.
The method cannot be a constructor,getter, or setter.
The method cannot have open generic types.
The method is not a method of the controller base class.
The method cannot contain ref or out parameters
Jayraj ChhayaPosted Oct 7, 2022, 5:06 PM
The action method must be public. It cannot be private or protected. Action method cannot be overloaded. Action method cannot be a static method.
The one restriction on action methods is that they have to be an instance method, so they cannot be static methods. Also, there are no return value restrictions. So you can return the string, integer, etc.