4

Is there an efficient way to get the control name "HelloWorld" when all I know is the class HelloWorldController?

Maybe something like:

var str = HelloWorldController.NetExtensionMethodWhichRetursActionName(); // str = "HelloWorld" Edit: can't exist as (currently) extension method only exist for instances

or

var str = NetUtilClass.MethodWhichRetursActionNameFromControllerclass(typeof(HelloWorldController)); // str = "HelloWorld"

or something else...

I know I could write something like:

string GetName<T>() where T : Controller { var s = typeof(T).Name; return s.Delete(s.Length - "Controller".Length; }

but I guess this functionality is already available in the framework.

Serge Intern
  • 2,669
  • 3
  • 22
  • 39

5 Answers5

3

DefaultHttpControllerSelector has a protected method to get the controller's name base on the default <controller name>Controllerclass naming convention.

Going as far as inheriting DefaultHttpControllerSelector in a utility class could do the trick. I don't need this anymore so I'm leaving the implementation to your imagination.

Serge Intern
  • 2,669
  • 3
  • 22
  • 39
2

There is no built-in function that will automatically do this for you, because putting "Controller" on the end of the name is just a common practice. It's not guaranteed that the controller will actually be named that way, so checking that the type is a Controller is not enough to ensure that the function will do what you want. For this reason, I would double think your design decision to go this route. What if someone decides to rename your controller at some point? What if whatever the controller name is based on changes, but the controller name doesn't?

If you do decide to go this route, you will have to write the function yourself. Putting it in a general utility class seems like the best bet. Another possible route is to create a class which extends Controller and implements this functionality, and then have your Controllers extend that instead of Controller directly.

Kat
  • 239
  • 2
  • 12
1

Please try below code.

var V=RouteData.Values.First().Value.ToString();

enter image description here

0

In the constructor (in C#) you can get the class name:

this.GetType().Name

In an action you can get the controller name in C# from the RouteData:

RouteData.Values["controller"]
anand
  • 1,559
  • 5
  • 21
  • 45
  • 5
    The type ends with Controller. I need the controller's name which doesn't. The controller's name is {controller} in the routing – Serge Intern Aug 05 '16 at 14:09
0

Why don't you just use:

string controllerName = nameof(HomeController).Replace("Controller", "");
//controllerName = "Home"

Update
For those who are obsessed with performance, the following code is about 3 times more performant:

string controllerName = nameof(HomeController);
controllerName = controllerName.Remove(controllerName.Length - 10);//10 is length of "Controller"
Bamdad
  • 726
  • 1
  • 11
  • 28
  • 1
    Because, as others have pointed out, there is no guarantee that the class name contains the controller name. I can define a class name Foo and give it a controller name of "Bar". – JamesQMurphy Jan 28 '21 at 15:13
  • 1
    @JamesQMurphy Well that would be out of standards. If you know what pattern you're using for naming your controllers, then you can modify the code to work properly. – Bamdad Jan 28 '21 at 17:38