main
wanggaofeng 1 year ago
parent e57bd3c22c
commit ed9c369007

@ -28,14 +28,19 @@ namespace HttpClientStudy.WebApp.Controllers
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpGet]
[HttpPost]
public async Task<IActionResult> GetWithBodyAsync() public async Task<IActionResult> GetWithBodyAsync()
{ {
using (var reader = new StreamReader(Request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true)) if (Request.ContentLength>0)
{ {
var body = await reader.ReadToEndAsync(); using (var reader = new StreamReader(Request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true))
// 现在你有了请求体,可以按照你的需求处理它 {
var body = await reader.ReadToEndAsync();
// 现在你有了请求体,可以按照你的需求处理它
}
} }
var reslut = BaseResultUtil.Success("操作成功"); var reslut = BaseResultUtil.Success("操作成功");
return Ok(reslut); return Ok(reslut);
} }

@ -40,8 +40,9 @@ namespace HttpClientStudy.WebApp
//配置Kestrel服务器选项 //配置Kestrel服务器选项
builder.Services.Configure<KestrelServerOptions>( option => builder.Services.Configure<KestrelServerOptions>( option =>
{ {
//允许同步 IO 操作(允许get方法有请求body) //(允许get方法有请求body)
//ASP.NET Core 3.0 之前的版本AllowSynchronousIO 默认是开启的 //ASP.NET Core 3.0 之前的版本AllowSynchronousIO 默认是开启的
//设置 true :允许同步 IO 操作,这样允许接收Get请求中的请求体数据.但只能直接从流中读取,不能自动模型绑定。
option.AllowSynchronousIO = true; option.AllowSynchronousIO = true;
}); });

Loading…
Cancel
Save