Browse Source
Merge pull request #21785 from abpframework/AbpNoContentActionFilter
Do not set `StatusCode` to `NoContent` if `Response.Body` has been written.
pull/21789/head
Engincan VESKE
1 year ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with
24 additions and
1 deletions
-
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Response/NoContentTestController.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Response/NoContentTestController_Tests.cs
|
|
|
@ -26,6 +26,12 @@ public class AbpNoContentActionFilter : IAsyncActionFilter, IAbpFilter, ITransie |
|
|
|
var returnType = context.ActionDescriptor.GetReturnType(); |
|
|
|
if (returnType == typeof(Task) || returnType == typeof(void)) |
|
|
|
{ |
|
|
|
if (context.HttpContext.Response.Body.CanSeek && context.HttpContext.Response.Body.Length != 0) |
|
|
|
{ |
|
|
|
// Body has been written, so we should not change the status code.
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.NoContent; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,4 +1,6 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
|
|
|
|
namespace Volo.Abp.AspNetCore.Mvc.Response; |
|
|
|
@ -55,4 +57,11 @@ public class NoContentTestController : AbpController |
|
|
|
{ |
|
|
|
await Task.CompletedTask; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
[Route("TestAsyncMethodChangeBody")] |
|
|
|
public async Task TestAsyncMethodChangeBody() |
|
|
|
{ |
|
|
|
await Response.Body.WriteAsync("TestBody"u8.ToArray()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -55,4 +55,12 @@ public class NoContentTestController_Tests : AspNetCoreMvcTestBase |
|
|
|
var result = await GetResponseAsync("/api/NoContent-Test/TestAsyncMethodWithResultFilter"); |
|
|
|
result.StatusCode.ShouldBe(HttpStatusCode.OK); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Not_Set_No_Content_If_Body_Changed() |
|
|
|
{ |
|
|
|
var result = await GetResponseAsync("/api/NoContent-Test/TestAsyncMethodChangeBody"); |
|
|
|
result.StatusCode.ShouldBe(HttpStatusCode.OK); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|