问题
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 best practice?
回答1:
T4MVC Does make action methods virtual. If you are using it, it should make action methods virtual, no other way it can work
回答2:
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.
回答3:
If you make them virtual then it will make the controllers easier to mock if you are using them in tests.
回答4:
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.
来源:https://stackoverflow.com/questions/6238068/should-i-make-my-asp-net-mvc-controller-actions-virtual