views:

52

answers:

1

Take the standard return statement for a controller:

return View("Index");

is there a way to make this thing compile time safe? using static reflection or some other trick?

+8  A: 

Yes; you're looking for this.

T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings when referring to controllers, actions and views. It helps make your MVC code much more maintainable, and gives you intellisense where you normally would not have any.

SLaks
If this does what I think it does this is awesome. I really hate all the literal strings and type unsafety that I've encountered since switching from .NET desktop applications to web application!!!
AaronLS
It generates a class hierarchy of string constants for action and controller names at compile-time using T4.
SLaks
does it also include something for the weakly typed routeValueDictionary/object passing?
Maslow
@Maslow since you can't tell what will be in a dictionary until run time, I don't see how this would be possible. Unit testing is your friend.
David Lively
well, at least up to an int for `Id` the examples on the linked page do that. not sure why that's the only type he is doing strong typing on.
Maslow
integrating this into an existing project I imagine is a bear =(
Maslow
OMG a bear!!!!!
AaronLS
@AaronLs - turns out... integration was spectacularly easy.. except the bug I ran into where it wanted a Name property added to all controllers, and I had a Controller Action called `Name`
Maslow