Josh
Josh

Reputation: 1970

Get the Controller Name and Method Name in ASP.NET-Core 2.2 Controller

I know that in asp.net-core 2.2, I can get the action name as follows:

ControllerContext.ActionDescriptor.ActionName

but I am looking for a way to get the controller name of the current request/page in asp.net-core 2.2

I will appreciate any guide.

Thank you

Upvotes: 12

Views: 21772

Answers (3)

Merak Marey
Merak Marey

Reputation: 799

There are situations where your ControllerContext will be null and you end up with nothing. I recommend using reflection.

this.GetType().Name

Upvotes: 0

Rdwan Alali
Rdwan Alali

Reputation: 189

I know It's late but maybe someone can benefit from it

ControllerName: ControllerContext.ActionDescriptor.ControllerName 
ActionName: ControllerContext.ActionDescriptor.ActionName

Upvotes: 9

Nkosi
Nkosi

Reputation: 247591

You have access to controller name in the

ControllerContext.ActionDescriptor.ControllerName

property, given that the action descriptor is of ControllerActionDescriptor type

Reference ControllerActionDescriptor.ControllerName

Upvotes: 21

Related Questions