我已经创建了一个自定义属性,并且试图在asp.net操作筛选器中检索此自定义属性的值,但是它似乎不可用。我究竟做错了什么?
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public sealed class MyCustomAttribute : Attribute
{
MyCustomAttribute(string param)
{
}
}
public class MyCustomActionFilter : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext context)
{
// unable to find my custom attribute here under context.Filters or anywhere else.
}
}
[HttpGet]
[MyCustomAttribute ("test123")]
public async Task<Details> GetDetails(
{
}
如果您想自己做,则要实现的目标要稍微复杂一点(即,通过的方法反映属性值Controller
)。在您的示例中,我建议使用ASP.NET Core的内置属性过滤器(有关更多信息,请参见ASP.NET Core文档):
public class MyCustomActionAttribute : ActionFilterAttribute
{
private readonly string param;
public MyCustomActionAttribute(string param)
{
this.param = param;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var paramValue = param;
base.OnActionExecuting(context);
}
}
并注释您的控制器操作,如下所示:
[HttpGet]
[MyCustomAction("test123")]
public async Task<Details> GetDetails()
{
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句