Skip to content

Writing HTML via BodyWriter is broken when VS.NET "Refresh browser after build" is on. #48897

@EamonNerbonne

Description

@EamonNerbonne

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-hot-reloadThis issue is related to the Hot Reload feaatureinvestigate

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions