In a constantly evolving MVC application, I would like to be able to get compiler checks on the correctness (or, at least, existence) of controller names and their action method names that are used to construct url's. So that
Url.Action("Index", "About")
becomes:
Url.Action("Index", Helper.GetControllerNameFromType(typeof(AboutController))
and it is guaranteed that AboutController type exists. I can deal with the controller name because from the type and its name the controller name can be obtained.
With the action method, however, I don't see a type-safe/compiler-checked way to get the method name. Any suggestion?
MvcContrib has an extension for UrlHelper. It should let you do this:
Url.Action<AboutController>(c => c.Index())
You could create an extension method that takes a generic or a type and get the name through convention. Something like
string ConventionBasedUrlAction(string action, Type controllerType)
{
return Url.Action(action, typeof(controllerType).Name.Replace("Controller", string.empty));
}
If it is an extension you can keep the Url. syntax and customize the convention in the code, while keeping the original alternatives.
EDIT: and MvcContrib already has it, as Tim pointed out :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With