如何在web api C#中的操作筛选器中获取post表单参数值?
如何使用web api中的请求读取post表单参数值?我有一个控制器如何在web api C#中的操作筛选器中获取post表单参数值?,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,如何使用web api中的请求读取post表单参数值?我有一个控制器 [Route("")] [HttpPost] [AuthenticationAttribute] public void PostQuery() { //some code } 我单独定义了AuthenticationAttribute类 public class AuthenticationAttribute : Attribute, IAuthenti
[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()
{
}
}
我想检查authenticateSync函数中的post参数
我试过了
context.Request.Content.ReadAsStringAsync().Result;
但是这个字符串是空的。我能够使用
context.Request.GetQueryNameValuePairs();
但找不到获取post表单参数的方法。非常感谢您的帮助。我不熟悉
context.Request.GetQueryNameValuePairs()
,但从名称上看,它听起来像是从查询字符串中提取参数。由于您正在执行POST
,因此查询字符串中没有参数(它们位于POST正文中)
试试这个:
context.HttpContext.Request.Params["groupId"]
或者这个:
context.Controller.ValueProvider.GetValue("groupId").AttemptedValue
这些工具的使用将取决于您如何实现模型和提供程序。使用
context.ActionContext.ActionArguments
这将得到一个字典
,其中键是参数的名称,值是参数本身
这假设操作方法使用模型绑定将传入的POST值绑定到模型
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
发送。请问您为什么要从AuthenticationAttribute访问请求正文?另一方面,我建议避免执行该任务