Last post Nov 03, 2014 11:40 AM by jens42
Nov 03, 2014 11:40 AM|jens42|LINK
As an alternative to T4MVC, I have written a small helper that allows the user to create urls in a name- and type-safe fashion without the need for automatically generated code:
return RedirectToAction<AccountController>(c => c.Login());
The helper is hosted as a gist and I discuss the implementation in a code project article.
It supports passing parameters:
Url.Action<MyController>(c => c.MyAction("somevalue", 42));
And also method evaluation inside the parameters:
Url.Action<MyController>(c => c.MyAction("some" + "value", Math.Round(42.4)));
What doesn't work, unfortunately, are named parameters:
Url.Action<MyController>(c => c.MyAction(s: "somevalue", n: 42)); // doesn't compile
My solution is worse than T4MVC performace-wise, but I prefer it because it's so light-weight: The helper is only about a hundred lines long and not dependent on T4.
It also has the advantage that renaming controller names, action names and parameter names (of actions) within Visual Studio properly renames all calls as well, something that can't work with a T4-based solution.
Hope it helps.