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

后端 未结 4 351
滥情空心
滥情空心 2021-01-12 05:19

File -> New Project for ASP.NET MVC projects used to generate controllers with virtual actions. I\'m not sure if that stopped with MVC 2 or MVC 3, but is this no longer a be

相关标签:
4条回答
  • 2021-01-12 05:44

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

    0 讨论(0)
  • 2021-01-12 05:49

    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.

    0 讨论(0)
  • 2021-01-12 06:01

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

    0 讨论(0)
  • 2021-01-12 06:08

    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.

    0 讨论(0)
提交回复
热议问题