sac*_*hin 5 c# asp.net asp.net-web-api
如何使用 Web api 中的请求读取帖子表单参数值?我有一个控制器
[Route("")]
[HttpPost]
[AuthenticationAttribute]
public void PostQuery()
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
我已经分别定义了 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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我想检查 AuthenticateAsync 函数中的 post 参数。
我试着做
context.Request.Content.ReadAsStringAsync().Result;
Run Code Online (Sandbox Code Playgroud)
但是这个字符串是空的。我能够使用读取查询参数
context.Request.GetQueryNameValuePairs();
Run Code Online (Sandbox Code Playgroud)
但是找不到获取帖子表单参数的方法。任何帮助表示赞赏。
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...
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码。我想检查参数skipExternal是True在post参数中发送的天气。
我不熟悉context.Request.GetQueryNameValuePairs(),但从名称来看,它听起来像是会从查询字符串中提取参数。由于您正在执行 a POST,因此查询字符串中没有参数(它们位于 POST 正文中)。
尝试这个:
context.HttpContext.Request.Params["groupId"]
Run Code Online (Sandbox Code Playgroud)
或这个:
context.Controller.ValueProvider.GetValue("groupId").AttemptedValue
Run Code Online (Sandbox Code Playgroud)
这些的使用取决于您如何实现模型和提供程序。
| 归档时间: |
|
| 查看次数: |
3182 次 |
| 最近记录: |