【发布时间】:2021-07-09 18:23:25
【问题描述】:
所以我有这个:
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next();
});
还有这个:
private async Task<JObject> GetRequestBodyAsync(HttpRequest request)
{
JObject objRequestBody = new JObject();
try
{
// IMPORTANT: Leave the body open so the next middleware can read it.
using (StreamReader reader = new StreamReader(
request.Body,
Encoding.UTF8,
detectEncodingFromByteOrderMarks: false,
leaveOpen: true))
{
string strRequestBody = await reader.ReadToEndAsync();
objRequestBody = JsonConvert.DeserializeObject<JObject>(strRequestBody);
}
}
finally
{
// IMPORTANT: Reset the request body stream position so the next middleware can read it
request.Body.Position = 0;
}
return objRequestBody;
}
但是虽然request.Body.Length > 0(如预期的那样),这行:
string strRequestBody = await reader.ReadToEndAsync();
总是返回一个空字符串。有什么想法吗?
接下来的操作检索[FromBody] OK。
【问题讨论】:
标签: asp.net-core-3.1