c# - AspNetCore.Mvc 中的描述符.ControllerDescriptor.ControllerName

标签 c# asp.net-core

我正在构建一个 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/

相关文章:

c# - 将 ASN.1 数据转换为公钥需要什么?例如如何确定 OID?

c# - 如何使用 C# .NET CORE 在 NSwag 文档中添加自定义 header ?

Asp.net core 客户端证书身份验证中间件在 X509Chain 构建上失败

c# - IIS 工作进程 : Which DLLs are loaded when different sites reference different versions?

c# - protobuf-net 如何处理只读字段?

c# - 如何通过电子邮件发送数据

azure - Identity Server 4在部署到Azure应用服务时找不到证书

c# - 如何使用 iTextSharp 将 C# DataGridView 导出为 PDF,同时**维护自定义货币格式**?

c# - 构造函数中的 HttpContext null

c# - ASP.NET 核心 : middleware to controller conversion issue?