c# - 当内容类型为application / x-www-form-urlencoded .net core时如何使用帖子数据
问题描述
我有一个用例,我需要接受application/x-www-form-urlencoded
发布数据并使用查询字符串
[httpPost]
public void GetDetails([FromQuery] username)
{
using (var reader = new StreamReader(Request.Body))
{
try
{
var line = await reader.ReadToEndAsync();
}
catch(exception ex)
{
//Do something
}
}
}
为此,当我尝试上面的Request.Body
流时null
,
我有一个解决方案来删除[FromQuery]
和读取查询字符串,Reqeust.QueryString
但我们还有其他选择吗
解决方案
[FromQuery]
并且[FromForm]
可以相互结合使用来绑定参数。
[HttpPost]
public IActionResult GetDetails([FromQuery] string username, [FromForm] string formData) {
//...Do something
return Ok();
}
虽然上面的示例采用字符串中的表单数据,但如果需要,它也可用于绑定到更复杂的对象。
ASP.NET Core 中的参考模型绑定
推荐阅读
- unit-testing - 对 16 位嵌入式目标的单元测试
- javascript - Google Ad Manager 网站的 CSP(内容安全政策)
- django - 从 Django 的 ImageField 下载并打印图像
- android - 本机崩溃发生了约 2k 次,但仅影响了 6 名使用 3 台特定三星设备的用户
- python - Python while 循环和 Reeborg 的世界挑战赛
- python - 将选项限制为仅整数
- python - Nextflow 在流程之间操纵变量
- cassandra - 如何在 MacOs Big Sur 上安装 Cassandra
- java - 如何在同一个项目中找到文件的路径?
- angular - 在 RxJS 管道中转换 JavaScript 对象