I am trying to change the body of specified requested files using code below. The index.html file is correctly sended to client, but other files included in head section are empty on client's side (files are shown in sources tab in chrome). Kestrel do not report any errors. Could you explain me why?
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();
}
};
}
Of course this function is registered in configuration file
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(ResponseStreamRewriter.Rewrite());
}