我正在构建一个 ASP.NET Core 2.0 Web 应用程序。在 ASP.NET WEB 中,我使用了 System.Web.Mvc,其中我有以下行来获取 ControllerName:
descriptor.ControllerDescriptor.ControllerName
在 ASP.NET Core 2.0 中这不起作用我得到错误:
Error CS1061 'ActionDescriptor' does not contain a definition for 'ControllerDescriptor' and no extension method 'ControllerDescriptor' accepting a first argument of type 'ActionDescriptor'
在 ASP.NET Core 2.0 中,我找不到获取 ControllerName 的替代方法。 有人有建议吗?
最佳答案
您必须将 ActionDescriptor
实例转换为 ControllerActionDescriptor
实例以访问 ControllerName
属性:
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (controllerActionDescriptor != null)
{
var controllerName = controllerActionDescriptor.ControllerName;
}
相关:How to read action method's attributes in ASP.NET Core MVC?
关于c# - AspNetCore.Mvc 中的描述符.ControllerDescriptor.ControllerName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876243/