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.

5
  • reflection? what? you need to provide more information. Commented Aug 5, 2016 at 5:41
  • what do you want to do? Commented Aug 5, 2016 at 5:43
  • do you want to list all action in the class Commented Aug 5, 2016 at 5:54
  • where do you want them in view ? Commented Aug 5, 2016 at 6:27
  • your question is not clear, where to get controller, how do you want to use, make more clear Commented Aug 5, 2016 at 6:30

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

Please try below code.

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

enter image description here

Comments

1

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"

2 Comments

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 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.
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"]

1 Comment

The type ends with Controller. I need the controller's name which doesn't. The controller's name is {controller} in the routing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.