.NET5 webApi项目接收HttpPost文本流数据

69
补充展位
Pages_Weblog_Get#3b21ac7b-c46a-4105-a711-ad4800b87fb4
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

.NET5 webApi项目接收HttpPost文本流数据

NET Framework 中web项目接收Post数据可以通过Request.InputStream 来取得,但在.NET 5中貌似行不通了。 以下是在.NET5中读取post内容的方法

//获取PostBody的文本流内容
string GetPostBody(HttpRequest Request)
{
            using (var buffer = new MemoryStream())
            {
                this.Request.Body.CopyTo(buffer);
                buffer.Position = 0;//一定要加上
                var reader = new StreamReader(buffer, System.Text.UTF8Encoding.Default);
                return reader.ReadToEndAsync().Result;
            }
}

如果运行出现以下错误:

Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead

解决办法:

public void ConfigureServices(IServiceCollection services) 方法内添加以下代码即可

services.Configure<IISServerOptions>(options =>
{
         options.AllowSynchronousIO = true;
});
补充展位 Pages_Weblog_Get#0
补充展位 Pages_Weblog_Get#1
补充展位 Pages_Weblog_Get#2
专题推荐
暂无内容
补充展位 Pages_Weblog_Get#3