我正在尝试创建一个自定义的ActionFilterAttribute,如图所示。该属性仅包含Path的属性。
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>
我装饰了这样的动作。
[TestLink(Path = "api/surveys/72469282/responses")]
public string GetQuestions(int id)
{
}
这对我来说是一个全新的领域,我做了一些研究,但不能/不知道是否有快速的方法来实现这一目标。目前输出为空,因为属性集合永远不会> 0
答案 0 :(得分:4)
进一步研究后,在使用继承自ApiController的控制器创建自定义属性时,必须从System.Web.Http.Filters继承。我从MVC名称空间(System.Web.MVC)继承标准的mvc ActionFilterAttribute。
using System.Web.Http.Filters;
namespace App.Extensions
{
public class TestLinkAttribute : ActionFilterAttribute
{
public string Path { get; set; }
}
}
现在,当我从HelpPage区域的ApiGroup.cshtml访问该属性时,我可以使用以下内容来正确获取该值。
var attrColl = api.ActionDescriptor.GetCustomAttributes<TestLinkAttribute>();