mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4 changed files with 142 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||
using System.Net; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc.Abstractions; |
|||
using Microsoft.AspNetCore.Mvc.Filters; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Response |
|||
{ |
|||
public class AbpNoContentActionFilter : IAsyncActionFilter, ITransientDependency |
|||
{ |
|||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) |
|||
{ |
|||
if (!context.ActionDescriptor.IsControllerAction()) |
|||
{ |
|||
await next(); |
|||
return; |
|||
} |
|||
|
|||
await next(); |
|||
|
|||
if (context.HttpContext.Response.StatusCode == (int)HttpStatusCode.OK) |
|||
{ |
|||
var returnType = context.ActionDescriptor.GetReturnType(); |
|||
if (returnType == typeof(Task) || returnType == typeof(void)) |
|||
{ |
|||
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.NoContent; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Response |
|||
{ |
|||
[Route("api/NoContent-Test")] |
|||
public class NoContentTestController : AbpController |
|||
{ |
|||
[HttpGet] |
|||
[Route("TestMethod")] |
|||
public void TestMethod() |
|||
{ |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("TestMethodWithReturn")] |
|||
public string TestMethodWithReturn() |
|||
{ |
|||
return "TestReturn"; |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("TestCustomHttpStatusCodeMethod")] |
|||
public void TestCustomHttpStatusCodeMethod() |
|||
{ |
|||
Response.Redirect("/"); |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("TestAsyncMethod")] |
|||
public async Task TestAsyncMethod() |
|||
{ |
|||
await Task.CompletedTask; |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("TestAsyncMethodWithReturn")] |
|||
public async Task<string> TestAsyncMethodWithReturn() |
|||
{ |
|||
return await Task.FromResult("TestReturn"); |
|||
} |
|||
|
|||
[HttpGet] |
|||
[Route("TestAsyncCustomHttpStatusCodeMethod")] |
|||
public async Task TestAsyncCustomHttpStatusCodeMethod() |
|||
{ |
|||
Response.Redirect("/"); |
|||
await Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
using System.Net; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Response |
|||
{ |
|||
public class NoContentTestController_Tests : AspNetCoreMvcTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Set_No_Content_For_Void_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestMethod", HttpStatusCode.NoContent) |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.NoContent); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Set_No_Content_For_Not_Void_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestMethodWithReturn") |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Set_No_Content_For_Custom_Http_Status_Code_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestCustomHttpStatusCodeMethod", HttpStatusCode.Redirect) |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.Redirect); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Set_No_Content_For_Task_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestAsyncMethod", HttpStatusCode.NoContent) |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.NoContent); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Set_No_Content_For_Not_Task_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestAsyncMethodWithReturn") |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Set_No_Content_For_Custom_Http_Status_Code_Async_Action() |
|||
{ |
|||
var result = await GetResponseAsync("/api/NoContent-Test/TestAsyncCustomHttpStatusCodeMethod", HttpStatusCode.Redirect) |
|||
.ConfigureAwait(false); |
|||
result.StatusCode.ShouldBe(HttpStatusCode.Redirect); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue