dotnet / systemweb-adapters

MIT License
335 stars 61 forks source link

Don't set content-type if not content #449

Closed jherven closed 9 months ago

jherven commented 9 months ago

Summary

Using this library in a dotnet core application forcefully adds content-type "text/html" (when not set) even if there's no content. This isn't always desired and I think it's neither expected when there is no content for the response.

Motivation and goals

Risks / unknowns

If somebody already using this library relies on this header always being set this change may break something

Detailed design

The change I've done for now is very simple.

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/SetDefaultResponseHeadersMiddleware.cs -> WriteDefaultContentType

Added new criteria "hasContent"

bool hasContent = context.Response.ContentLength.HasValue;
        if (hasContent && context.Response.Headers.ContentType.Count == 0)
        {
            context.Response.Headers.ContentType = "text/html";
        }
twsouthwick commented 9 months ago

Thanks @jherven - have you verified this is the same behavior as on ASP.NET Framework? If so, would you be willing to submit a PR?

jherven commented 9 months ago

Yes, for the scenarios I tested this change better mimics ASP.NET framework. PR created #450 .