-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
HTML Responses written via HttpContext.Response.BodyWriter fail (returning empty content) when the asp.net core application is started from VS.NET and the VS.NET option "Projects and Solutions > ASP.NET Core > General > Auto build and refresh" is set to "Refresh browser on build".
This is easily triggered in various configurations, but for example, the following code derived from the ASP.NET core Web API template will trigger it:
using System.Net;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGet(
"/html",
async context => {
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "text/html; charset=utf-8";
new UTF8Encoding(false).GetBytes("Hello, world!", context.Response.BodyWriter);
//await context.Response.BodyWriter.FlushAsync(); //Required when "Refresh browser after build" is on.
}
);
app.Run();Initial analysis shows that something somewhere is trying to inject a script tag referring to aspnetcore-browser-refresh.js. I'm not sure why this results in the response received by the browser being empty.
I tried to find an existing issue for this behavior, and found the following possibly related but non-identical issues:
- Revisit hot reload browser refresh script injection #45213
- Streaming rendering is blocked by CSS Hot Reload (possibly by browserlink) #47608
Expected Behavior
Loading /html of the provided sample in a browser displays a page containing Hello, world!, even when the web server is started directly from VS.NET. Alternatively, if some configuration combination cannot be supported, I'd expect an exception to be thrown and/or a message to be logged somewhere visible.
Steps To Reproduce
using System.Net;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGet(
"/html",
async context => {
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "text/html; charset=utf-8";
new UTF8Encoding(false).GetBytes("Hello, world!", context.Response.BodyWriter);
//await context.Response.BodyWriter.FlushAsync(); //Required when "Refresh browser after build" is on.
}
);
app.Run();Exceptions (if any)
No response
.NET Version
7.0.304
Anything else?
Reproducible with both VS.NET Version 17.7.0 Preview 2.0 and Version 17.6.3