6

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?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Maslow
  • 18,464
  • 20
  • 106
  • 193

1 Answers1

8

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    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 Apr 07 '10 at 20:19
  • 1
    It generates a class hierarchy of string constants for action and controller names at compile-time using T4. – SLaks Apr 07 '10 at 20:20
  • does it also include something for the weakly typed routeValueDictionary/object passing? – Maslow Apr 07 '10 at 20:24
  • @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. – 3Dave Apr 07 '10 at 20:26
  • 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 Apr 07 '10 at 20:29
  • integrating this into an existing project I imagine is a bear =( – Maslow Apr 07 '10 at 20:30
  • @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 Apr 09 '10 at 22:11