11

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?

1 Answer 1

23

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?

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

Comments

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.