gpt4 book ai didi

c# - 如何在 Web api,C# 中的 Action 过滤器中获取帖子表单参数值?

转载 作者:行者123 更新时间:2023-11-30 17:34:35 27 4
gpt4 key购买 nike

如何使用 Web API 中的 Request 读取帖子表单参数值?我有一个 Controller

    [Route("")]
[HttpPost]
[AuthenticationAttribute]
public void PostQuery()
{
//some code
}

我单独定义了AuthenticationAttribute类

 public class AuthenticationAttribute : Attribute, IAuthenticationFilter
{


public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
// I want to read the post paramter values over here
}

public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
return Task.Run(
() =>
{

});
}

public AuthenticationAttribute()
{

}
}

我想检查 AuthenticateAsync 函数中的 post 参数。

我试过

context.Request.Content.ReadAsStringAsync().Result;

但是这个字符串是空的。我能够使用

读取查询参数
context.Request.GetQueryNameValuePairs();

但找不到获取帖子表单参数的方法。感谢您的帮助。

最佳答案

var reader = new StreamReader(HttpContext.Current.Request.InputStream);
var content = reader.ReadToEnd();
var jObj = (JObject)JsonConvert.DeserializeObject(content);

foreach (JToken token in jObj.Children())
{
if (token is JProperty)
{
var prop = token as JProperty;

if (prop.Name.Equals("skipExternal") && prop.Value.ToString().Equals("True"))
{
// Logic...
}
}
}

这是我使用的代码。我想检查参数 skipExternal 是否在 post 参数中作为 True 发送。

关于c# - 如何在 Web api,C# 中的 Action 过滤器中获取帖子表单参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236756/

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