From e57bd3c22cb1fed2ea0a6da61a52ed1b39c58ea6 Mon Sep 17 00:00:00 2001 From: wanggaofeng <15601716045@163.com> Date: Fri, 5 Jan 2024 14:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AdvancedGetController.cs | 43 +++++++++++++++++++ HttpClientStudy.WebApp/Program.cs | 9 ++++ 2 files changed, 52 insertions(+) create mode 100644 HttpClientStudy.WebApp/Controllers/AdvancedGetController.cs diff --git a/HttpClientStudy.WebApp/Controllers/AdvancedGetController.cs b/HttpClientStudy.WebApp/Controllers/AdvancedGetController.cs new file mode 100644 index 0000000..df5bb05 --- /dev/null +++ b/HttpClientStudy.WebApp/Controllers/AdvancedGetController.cs @@ -0,0 +1,43 @@ +using System.Text; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace HttpClientStudy.WebApp.Controllers +{ + /// + /// 高级Get请求 控制器 + /// + [Route("api/[controller]/[action]")] + [ApiController] + public class AdvancedGetController : ControllerBase + { + private ILogger _logger; + + /// + /// 构造 + /// + public AdvancedGetController(ILogger logger) + { + _logger = logger; + } + + /// + /// 带请求体数据的Get请求 + /// (asp.net 3开始,默认不允许Get有Body) + /// + /// + [HttpGet] + public async Task GetWithBodyAsync() + { + using (var reader = new StreamReader(Request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true)) + { + var body = await reader.ReadToEndAsync(); + // 现在你有了请求体,可以按照你的需求处理它 + } + + var reslut = BaseResultUtil.Success("操作成功"); + return Ok(reslut); + } + } +} diff --git a/HttpClientStudy.WebApp/Program.cs b/HttpClientStudy.WebApp/Program.cs index 4ca0d05..536753f 100644 --- a/HttpClientStudy.WebApp/Program.cs +++ b/HttpClientStudy.WebApp/Program.cs @@ -3,6 +3,7 @@ using System.Text; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; @@ -36,6 +37,14 @@ namespace HttpClientStudy.WebApp option.IdleTimeout = TimeSpan.FromHours(1); }); + //Kestrelѡ + builder.Services.Configure( option => + { + //ͬ IO (getbody) + //ASP.NET Core 3.0 ֮ǰİ汾AllowSynchronousIO Ĭǿ + option.AllowSynchronousIO = true; + }); + //Formύѡ builder.Services.Configure(options => {