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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With