Skip to content

Commit 2e9a5c0

Browse files
Stop setting content length 0 on 204s in MVC. (#43360)
1 parent 8616fbc commit 2e9a5c0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Mvc/Mvc.Core/src/Formatters/HttpNoContentOutputFormatter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public bool CanWriteResult(OutputFormatterCanWriteContext context)
3434
public Task WriteAsync(OutputFormatterWriteContext context)
3535
{
3636
var response = context.HttpContext.Response;
37-
response.ContentLength = 0;
3837

3938
if (response.StatusCode == StatusCodes.Status200OK)
4039
{

src/Mvc/Mvc.Core/test/Formatters/NoContentFormatterTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ public async Task WriteAsync_WritesTheStatusCode204()
130130
Assert.Equal(StatusCodes.Status204NoContent, context.HttpContext.Response.StatusCode);
131131
}
132132

133+
[Fact]
134+
public async Task WriteAsync_DoesNotHaveContentLengthSet()
135+
{
136+
// Arrange
137+
var context = new OutputFormatterWriteContext(
138+
new DefaultHttpContext(),
139+
new TestHttpResponseStreamWriterFactory().CreateWriter,
140+
typeof(string),
141+
@object: null);
142+
143+
var formatter = new HttpNoContentOutputFormatter();
144+
145+
// Act
146+
await formatter.WriteAsync(context);
147+
148+
// Assert
149+
// No Content responses shouldn't have a Content-Length.
150+
Assert.Null(context.HttpContext.Response.ContentLength);
151+
}
152+
133153
[Fact]
134154
public async Task WriteAsync_ContextStatusCodeSet_WritesSameStatusCode()
135155
{

0 commit comments

Comments
 (0)