Should I make my ASP.NET MVC controller actions virtual?

江枫思渺然 提交于 2019-12-01 04:06:10
archil

T4MVC Does make action methods virtual. If you are using it, it should make action methods virtual, no other way it can work

The current documentation for ASP.NET MVC 3 does not show virtual methods. I'm not really sure what making them virtual would gain you, as I've never subclassed controllers to override actions.

If you make them virtual then it will make the controllers easier to mock if you are using them in tests.

Generated code may be made virtual for various reasons. It's not good practice to make your own code virtual unless required by some tool. See Liskov Substitution Principle and Open/Closed Principle. I think some frameworks do this to facilitate creating proxies but I can't imagine any reason to make all of your methods virtual. To me it screams copy/paste or cargo cult programmer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!