C# MVC Web API 2-在操作中使用自定义属性

C# MVC Web API 2-在操作中使用自定义属性,c#,asp.net-mvc,asp.net-web-api,actionfilterattribute,C#,Asp.net Mvc,Asp.net Web Api,Actionfilterattribute,我正在尝试创建自定义ActionFilterAttribute,如图所示。该属性将仅包含路径的属性 public class TestLinkAttribute : ActionFilterAttribute { public string Path { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { } } 我希望能够访问w

我正在尝试创建自定义ActionFilterAttribute,如图所示。该属性将仅包含路径的属性

public class TestLinkAttribute : ActionFilterAttribute
{
    public string Path { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}
我希望能够访问web api集成的帮助页面区域中的此属性,类似于此

<td class="api-testLink">
@{ 
var attrColl = api.ActionDescriptor.GetCustomAttributes<TestLinkAttribute>();
      if(attrColl.Count > 0)
      {
          <p>@attrColl[0].Path</p>
      }
}
</td>

这对我来说是一个全新的领域,我做了一些研究,但不知道是否有一个快速的方法来实现这一点。当前输出为空,因为属性集合从不>0

经过进一步研究,当使用继承自ApicController的控制器创建自定义属性时,必须继承自System.Web.Http.Filters。我从mvc名称空间(System.Web.mvc)继承了标准的mvc ActionFilterAttribute

现在,当我从HelpPage区域中的ApiGroup.cshtml访问属性时,我可以使用以下命令,它将正确地获取值

var attrColl = api.ActionDescriptor.GetCustomAttributes<TestLinkAttribute>();
var attrColl=api.ActionDescriptor.GetCustomAttributes();

我构建了一个简单的版本,创建了一个自定义属性,将其分配给一个动作,然后拉出该属性以显示一个
路径
属性,如图所示。在我的示例解决方案中,这一切都起作用。您是否能够提供一个不起作用的示例并将其托管在Github上?不幸的是,我目前不能。这是一个内部应用程序,公司在外部托管这类东西时非常严格。是否有其他必要的东西,因为我正在尝试访问此应用程序的帮助区域内的属性以及带有反射的东西?我可以在那个区域之外做这件事,没问题。我想不出其他的事了。问题可能出在你的问题中没有详细说明的地方。
using System.Web.Http.Filters;

namespace App.Extensions
{
    public class TestLinkAttribute : ActionFilterAttribute
    {
        public string Path { get; set; }

    }
}
var attrColl = api.ActionDescriptor.GetCustomAttributes<TestLinkAttribute>();