asp.net - 将变量从 Web API Controller 方法传递到过滤器

标签 asp.net asp.net-web-api action-filter

我想将 appId 变量值发送到过滤器

// GET api/filter
[CustomFilter]
public IEnumerable<string> Get()
{

   var  appId = 123;
    return new string[] { "value1", "value2" };
}

我可以使用 OnActionExecuting 或 OnActionExecuted 方法

public override void OnActionExecuting(HttpActionContext actionContext)
{
            base.OnActionExecuting(actionContext);

           //here i want to access appId value

}

我知道如何使用查询字符串访问参数值

最佳答案

在 Controller 操作方法中,设置请求对象的 Properties 字典中的值,如下所示:Request.Properties["AppId"] = 123;

在过滤器的 OnActionExecuted 方法中,按如下方式检索它:actionContext.Request.Properties["AppId"]

顺便说一句,如果在操作方法中设置了值,则必须使用过滤器的 OnActionExecuted 方法。 OnActionExecuting 方法在执行操作方法之前运行。

关于asp.net - 将变量从 Web API Controller 方法传递到过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25081946/

相关文章:

C# ASP.NET 自定义控件未显示

c# - Web Api 的 PostAsync HttpClient 错误 - System.AggregateException "A task was canceled."

javascript - 如何将对象从 Angular 发布到 webapi Controller

c# - 从更新面板中的表中获取动态添加的文本框的值

c# - 在数据库中创建新记录

c# - 尝试将长字符串提交给 Asp.Net Web Api 2 Controller 方法时出现错误请求

.net - 如何在 ASP.NET Web Api 中对操作过滤器进行单元测试?

c# - ASP NET Core TypeFilter 属性未使用过滤器属性提供的值进行设置

c# - 在 ASP.NET 中为特定页面设置无缓存?