我正在尝试使用下面的代码更改指定的请求文件的正文。 index.html文件被正确发送到客户端,但head部分中包含的其他文件在客户端是空的(文件显示在chrome中的sources选项卡中)。红隼不报告任何错误。你能解释一下为什么吗?
public static Func<HttpContext, Func<Task>, Task> Rewrite()
{
return async (context, next) =>
{
if (context.Request.Path.Value == "/")
{
var originalStream = context.Response.Body;
using (Stream bufferStream = new FileStream("./wwwroot/index.html", FileMode.Open, FileAccess.Read))
{
context.Response.Body = bufferStream;
bufferStream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(bufferStream))
{
var response = await reader.ReadToEndAsync();
response = response.Replace("[ddd]", "<script type=\"text/javascript\" src=\"lib/jquery/dist/jquery.js\"></script>");
using (var writer = new StreamWriter(originalStream))
{
await writer.WriteAsync(response);
}
}
}
}
else
{
await next();
}
};
}
当然,此功能已在配置文件中注册
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(ResponseStreamRewriter.Rewrite());
}