加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – AspNetCore.Mvc中的descriptor.ControllerDescriptor.Cont

发布时间:2020-12-15 23:40:31 所属栏目:百科 来源:网络整理
导读:我正在构建一个ASP.NET Core 2.0 Web应用程序.在ASP.NET WEB中我使用了System.Web.Mvc,其中我有以下行来获取ControllerName: descriptor.ControllerDescriptor.ControllerName 在ASP.NET Core 2.0中,这不起作用我收到错误: Error CS1061 ‘ActionDescripto
我正在构建一个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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读