Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create Static ActionResult method in Controller?

Tags:

asp.net-mvc

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?

like image 956
rajk Avatar asked Dec 27 '22 07:12

rajk


2 Answers

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

like image 52
Aleksandar Vucetic Avatar answered Feb 11 '23 11:02

Aleksandar Vucetic


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.

like image 33
superwalnut Avatar answered Feb 11 '23 11:02

superwalnut