Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 621
Josh Avatar asked Aug 31 '19 22:08

Josh


People also ask

How can we call controller method from view in ASP.NET Core?

You should not call a controller from the view. Add a property to your view model, set it in the controller, and use it in the view.

How do I find my controller name on Web API?

3 Answers. Show activity on this post. You can decorate each method that needs logging with [Logger] or do it at the controller level to log every single call that happens inside that controller. Lastly, you can make your action filter global so that it will run everytime any action is called inside your project.


2 Answers

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

ControllerName: ControllerContext.ActionDescriptor.ControllerName 
ActionName: ControllerContext.ActionDescriptor.ActionName
like image 84
Rdwan Alali Avatar answered Oct 23 '22 07:10

Rdwan Alali


You have access to controller name in the

ControllerContext.ActionDescriptor.ControllerName

property, given that the action descriptor is of ControllerActionDescriptor type

Reference ControllerActionDescriptor.ControllerName

like image 20
Nkosi Avatar answered Oct 23 '22 07:10

Nkosi