ASP.NET MVC 3 : Is there a way to get a controller's string Name from its Type?

RAdamWilliams

New Member
With the \[code\]UrlHelper\[/code\] in MVC3, you can build a URL for a controller and an action using strings:\[code\]@Url.Action("Index", "Somewhere")\[/code\]Which will route a request to \[code\]SomewhereController.Index()\[/code\].What I would like to do, is get a URL to a controller and action, but passing the Type for the controller:\[code\]@Url.Action("Index", typeof(SomewhereController))\[/code\]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 \[code\]{Name}Controller\[/code\] 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 \[code\]SomewhereFoo\[/code\] instead of \[code\]SomewhereController\[/code\], 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.
 
Back
Top