gpt4 book ai didi

c# - 如何读取 web api 操作过滤器中的参数属性值

转载 作者:行者123 更新时间:2023-11-30 14:49:05 25 4
gpt4 key购买 nike

我有一个 API 方法,其中 Id 参数用 CacheType 属性注释

public Object Get([CacheType(CacheTypes.Venue)]int Id)
{
....
}

我可以读取ActionFilterAttribute里面参数属性的值吗

public class CacheOutputAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//read CacheType value
}
}

最佳答案

要访问当前执行方法的参数集合,您调用

actionContext.ActionDescriptor.GetParameters()

您可以遍历 HttpParameterDescriptor 的集合并找到您需要的参数。您可以通过名称、索引或任何您认为合适的方式来完成。

然后,您可以使用方法GetCustomAttributes<TClass>()HttpParameterDescriptor 类型的对象中定义检查参数是否标记有 TClass 类型的属性.如果您需要属性的实例来检查值,只需从生成的属性集合(如果找到)中获取它即可。

关于c# - 如何读取 web api 操作过滤器中的参数属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com