Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

descriptor.ControllerDescriptor.ControllerName in AspNetCore.Mvc

I'm building an ASP.NET Core 2.0 Web Application. In ASP.NET WEB I used System.Web.Mvc where I had the following line to get the ControllerName:

descriptor.ControllerDescriptor.ControllerName

In ASP.NET Core 2.0 this does not work I get the error:

Error CS1061 'ActionDescriptor' does not contain a definition for 'ControllerDescriptor' and no extension method 'ControllerDescriptor' accepting a first argument of type 'ActionDescriptor'

In ASP.NET Core 2.0 I can't find an alternative to get the ControllerName. Does anyone have a suggestion?

like image 639
FerronSwitch Avatar asked Aug 25 '17 07:08

FerronSwitch


1 Answers

You'll have to cast the ActionDescriptor instance to a ControllerActionDescriptor instance in order to get access to the ControllerName property:

var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (controllerActionDescriptor != null)
{
    var controllerName = controllerActionDescriptor.ControllerName;
}

Related: How to read action method's attributes in ASP.NET Core MVC?

like image 182
Henk Mollema Avatar answered Nov 07 '22 07:11

Henk Mollema