diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs
index e7fe04d9d4..fb7f7cbb2f 100644
--- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs
+++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs
@@ -332,9 +332,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public unsafe void SendResponseHeaders(bool appCompleted)
{
// Verifies we have sent the statuscode before writing a header
- var reasonPhrase = string.IsNullOrWhiteSpace(ReasonPhrase) ? ReasonPhrases.GetReasonPhrase(StatusCode) : ReasonPhrase;
- var reasonPhraseBytes = Encoding.UTF8.GetBytes(reasonPhrase);
-
+ var reasonPhrase = string.IsNullOrEmpty(ReasonPhrase) ? ReasonPhrases.GetReasonPhrase(StatusCode) : ReasonPhrase;
+ var reasonPhraseBytes = new byte [reasonPhrase.Length + 1];
+ Encoding.ASCII.GetBytes(reasonPhrase, 0, reasonPhrase.Length, reasonPhraseBytes, 0);
+
fixed (byte* pReasonPhrase = reasonPhraseBytes)
{
Debug.Assert((IntPtr)pReasonPhrase != IntPtr.Zero);
diff --git a/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj b/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj
index 77f3646a70..c09eeee095 100644
--- a/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj
+++ b/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj
@@ -10,6 +10,7 @@
+
diff --git a/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs b/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs
index 8cee6643a4..e54920e59d 100644
--- a/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs
+++ b/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs
@@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
Assert.Equal("1", headerValues.First());
Assert.Equal("2", headerValues.Last());
}
-
+
[ConditionalFact]
public async Task ErrorCodeIsSetForExceoptionDuringRequest()
{
@@ -48,5 +48,17 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal("Internal Server Error", response.ReasonPhrase);
}
+
+ [ConditionalTheory]
+ [InlineData(1, "custom", "custom")]
+ [InlineData(500, "", "Internal Server Error")]
+ [InlineData(999, "", "")]
+ public async Task CustomErrorCodeWorks(int code, string reason, string expectedReason)
+ {
+ var response = await _fixture.Client.GetAsync($"SetCustomErorCode?code={code}&reason={reason}");
+ Assert.Equal((HttpStatusCode)code, response.StatusCode);
+ Assert.Equal(expectedReason, response.ReasonPhrase);
+ Assert.Equal("Body", await response.Content.ReadAsStringAsync());
+ }
}
}
diff --git a/test/IISTestSite/Startup.cs b/test/IISTestSite/Startup.cs
index a74524587a..28460cdaf5 100644
--- a/test/IISTestSite/Startup.cs
+++ b/test/IISTestSite/Startup.cs
@@ -218,6 +218,16 @@ namespace IISTestSite
app.Run(ctx => { throw new Exception(); });
}
+ private void SetCustomErorCode(IApplicationBuilder app)
+ {
+ app.Run(async ctx => {
+ var feature = ctx.Features.Get();
+ feature.ReasonPhrase = ctx.Request.Query["reason"];
+ feature.StatusCode = int.Parse(ctx.Request.Query["code"]);
+ await ctx.Response.WriteAsync("Body");
+ });
+ }
+
private void HelloWorld(IApplicationBuilder app)
{
app.Run(async ctx =>