With the UrlHelper
in MVC3, you can build a URL for a controller and an action using strings:
@Url.Action("Index", "Somewhere")
Which will route a request to SomewhereController.Index()
.
What I would like to do, is get a URL to a controller and action, but passing the Type for the controller:
@Url.Action("Index", typeof(SomewhereController))
Is there a way to do this?
Edit / Clarification:
I realize the convention for the Controllers is that the controller name routes to a class named {Name}Controller
so I could just remove 'Controller' from the end of my Type.Name. I guess I was assuming that there was a way to override this convention with some custom routing. Though the more I look at it, I'm not sure that is possible...
Maybe MVC3 can only ever route to classes named "*Controller"? I'm combing through the MVC3 source looking for "Controller" hard coded somewhere, but haven't found the answer yet... But if it is possible to route "Somewhere" to the class SomewhereFoo
instead of SomewhereController
, then just removing "Controller" from the class name would be incorrect.
If someone can give me evidence for or against "Controller" being hard-coded into MVC3 somewhere, then I would feel more comfortable with the "Just remove Controller from the name" approach.