[英]ASP.NET Core modify/substitute request body
I want to do a substitution on HttpContext.Request.Body
.我想对
HttpContext.Request.Body
进行替换。
I've tried to do it inside a middleware:我试图在中间件中做到这一点:
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value.Contains("DataSourceResult"))
{
var originalBody = new StreamReader(context.Request.Body).ReadToEnd();
DataSourceRequest dataSource = null;
try
{
dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalBody);
} catch
{
await _next.Invoke(context);
}
if (dataSource != null && dataSource.Take > 2000)
{
dataSource.Take = 2000;
var bytesToWrite = dataSource.AsByteArray();
await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
}
else
{
var bytesToWrite = originalBody.AsByteArray();
await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
}
}
await _next.Invoke(context);
}
The first problem is that the body can be read only once, and secondly, the stream is read-only and can't be written to.第一个问题是body只能读取一次,其次流是只读的,不能写入。
How can I modify/substitute Request.Body
?如何修改/替换
Request.Body
? I need to change property value of request body.我需要更改请求正文的属性值。
Take the request body, read its content, make whatever changes are necessary if at all, then create a new stream to pass down the pipeline.获取请求正文,读取其内容,进行任何必要的更改(如果有的话),然后创建一个新流以向下传递管道。 Once accessed, the request stream has to be replaced.
一旦访问,请求流必须被替换。
public async Task Invoke(HttpContext context) {
var request = context.Request;
if (request.Path.Value.Contains("DataSourceResult")) {
//get the request body and put it back for the downstream items to read
var stream = request.Body;// currently holds the original stream
var originalContent = new StreamReader(stream).ReadToEnd();
var notModified = true;
try {
var dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalContent);
if (dataSource != null && dataSource.Take > 2000) {
dataSource.Take = 2000;
var json = JsonConvert.SerializeObject(dataSource);
//replace request stream to downstream handlers
var requestContent = new StringContent(json, Encoding.UTF8, "application/json");
stream = await requestContent.ReadAsStreamAsync();//modified stream
notModified = false;
}
} catch {
//No-op or log error
}
if (notModified) {
//put original data back for the downstream to read
var requestData = Encoding.UTF8.GetBytes(originalContent);
stream = new MemoryStream(requestData);
}
request.Body = stream;
}
await _next.Invoke(context);
}
In my case it was also necessery to update Request.ContentLength to new value.就我而言,还需要将 Request.ContentLength 更新为新值。 My solution is :
我的解决方案是:
string originalContent;
using (StreamReader stream = new StreamReader(context.Request.Body))
{
originalContent = stream.ReadToEnd();
}
var dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalContent);
if (dataSource != null && dataSource.Take > 2000)
dataSource.Take = 2000;
string json = JsonConvert.SerializeObject(dataSource);
var requestData = Encoding.UTF8.GetBytes(json);
context.Request.Body = new MemoryStream(requestData);
context.Request.ContentLength = context.Request.Body.Length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.