rajk
rajk

Reputation: 221

Can we create Static ActionResult method in Controller?

Someone ask following question to me in interview, to which I cannot able to answer. Somebody please help me.

Can we create Static ActionResult method in controller? If yes. What is the benifits? and if no. What is the reason?

Upvotes: 5

Views: 5588

Answers (2)

Aleksandar Vucetic
Aleksandar Vucetic

Reputation: 14953

If "ActionResult 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

Upvotes: 10

superwalnut
superwalnut

Reputation: 319

Don't think it is a good idea to create a static action method, as you will no longer have access to all the controller properties, such as controllercontext, etc.

Upvotes: 2

Related Questions