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

3

8

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?

Jollify answered 4/6, 2011 at 16:52 Comment(1)
Interesting question. I hadn't noticed the change but now that you mention it, that does sound vaguely familiar.Eakins
D
8

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

Dependence answered 4/6, 2011 at 20:30 Comment(3)
yeah, I realized that, too. I have been wondering way though. Should dig into the features of virtualRunty
Making action methods virtual roughly saying is not the feature of asp.net mvc. That's more the c# language feature that T4MVC usesDependence
this was it. T4MVC added the virtual keyword to any action that didn't already have it. it also made my controllers partial classesJollify
E
3

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.

Eakins answered 4/6, 2011 at 19:22 Comment(0)
G
1

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.

Ginaginder answered 19/2, 2016 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.