小编Eri*_*ric的帖子

MVC Web API 2 - 在动作上使用自定义属性

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

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

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在 web api 集成的帮助页面区域上访问此属性。

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

我装饰了这样的动作。

 [TestLink(Path = "api/surveys/72469282/responses")]
 public string GetQuestions(int id)
 {
 }
Run Code Online (Sandbox Code Playgroud)

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

c# asp.net-mvc actionfilterattribute asp.net-web-api

3
推荐指数
1
解决办法
6084
查看次数