0

The HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString() Indeed return the current controller name but it return it as the user type it.

For example, my app contains "ImageGallery" controller and if the user type http://localhost/imagegallery to get it - then I get "imagegallery" from the RouteData.Values instead of "ImageGallery".

How can I get the original controller name?

tereško
  • 58,060
  • 25
  • 98
  • 150
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

2 Answers2

0

You get what the user entered because it does not matter typically (unless you are Linux/Unix). However, if your problem deals with the routes not resolving properly, then maybe this StackOverflow question is what you are looking for?

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I want to translate it from my Resource file and it's case-sensitive. – Yair Nevet Mar 10 '12 at 15:31
  • I do not believe that you can change this as you get whatever the user entered. The best suggestion I have seen is to use a regex to deal with the case, otherwise. – Justin Pihony Mar 10 '12 at 15:40
0

Perhaps you can get the actual CLR instance of your controller, and get the name from there on your view...

@if (this.ViewContext.Controller is MyController)
{
}

and perhaps strip the "Controller" part of it's name... like

this.ViewContext.Controller.GetType().Name.Replace("Controller", "")

PS: Used .Replace(string, string) for the sake of simplicity of this post, but it's probably better to use a .Substring(int, int) call.

EDIT: You can also get the current controller from the Controller context if you were not in a View (though you'd be in the controller, in which case this.GetType() would do it.

Mauricio Morales
  • 988
  • 1
  • 9
  • 16