Skip to content

Commit 0a2868f

Browse files
committed
opt: 添加中间件选项IgnoreOptionsRequest以忽略OPTIONS请求
1 parent d5fa5d4 commit 0a2868f

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

src/Cuture.AspNetCore.ResponseAutoWrapper/Cuture.AspNetCore.ResponseAutoWrapper.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<IsPackable>true</IsPackable>
1717
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1818

19-
<Version>1.2.1</Version>
19+
<Version>1.2.2</Version>
2020
<Description>Response and exception automatic wrapper for `asp.net core` to provide a consistent response content format for `Action`. 用于`asp.net core`的响应和异常自动包装器,使`Action`提供一致的响应内容格式。</Description>
2121

2222
<PackageIdPrefix>Cuture.AspNetCore.ResponseAutoWrapper</PackageIdPrefix>

src/Cuture.AspNetCore.ResponseAutoWrapper/Extensions/ResponseDescriptionHttpContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static bool TryGetResponseDescription<TCode, TMessage>(this ResultExecuti
106106
#region Private 方法
107107

108108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109-
private static Exception DescriptionTypeNotMatchException<TCode, TMessage>()
109+
private static InvalidOperationException DescriptionTypeNotMatchException<TCode, TMessage>()
110110
{
111111
return new InvalidOperationException($"The http context has description object. But it's not the instance of {typeof(ResponseDescription<TCode, TMessage>)}. This situation is likely to be the description type use error. Please check it.");
112112
}

src/Cuture.AspNetCore.ResponseAutoWrapper/ResponseAutoWrapMiddleware.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ internal class ResponseAutoWrapMiddleware
4141

4242
private readonly IHttpResponseStreamWriterFactory _httpResponseStreamWriterFactory;
4343

44+
/// <inheritdoc cref="ResponseAutoWrapMiddlewareOptions.IgnoreOptionsRequest"/>
45+
private readonly bool _ignoreOptionsRequest;
46+
4447
private readonly OutputFormatterSelector _outputFormatterSelector;
4548

4649
#endregion OutputFormat
@@ -86,6 +89,7 @@ public ResponseAutoWrapMiddleware(RequestDelegate next,
8689

8790
_notCatchExceptions = !options.CatchExceptions;
8891
_throwCaughtExceptions = options.ThrowCaughtExceptions;
92+
_ignoreOptionsRequest = options.IgnoreOptionsRequest;
8993

9094
var delegateCollection = GetService<ResponseAutoWrapperWorkDelegateCollection>();
9195

@@ -101,6 +105,13 @@ public ResponseAutoWrapMiddleware(RequestDelegate next,
101105

102106
public async Task InvokeAsync(HttpContext context)
103107
{
108+
if (_ignoreOptionsRequest
109+
&& HttpMethods.IsOptions(context.Request.Method))
110+
{
111+
await _next(context);
112+
return;
113+
}
114+
104115
try
105116
{
106117
await _next(context);

src/Cuture.AspNetCore.ResponseAutoWrapper/ResponseAutoWrapMiddlewareOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public class ResponseAutoWrapMiddlewareOptions
2727
= static formatters => formatters.FirstOrDefault(m => m.GetType() == typeof(SystemTextJsonOutputFormatter))
2828
?? throw new InvalidOperationException($"Can not found {nameof(SystemTextJsonOutputFormatter)} by default. Must select a formatter manually by \"{nameof(ResponseAutoWrapMiddlewareOptions)}.{nameof(DefaultOutputFormatterSelector)}\" at middleware setup.");
2929

30+
/// <summary>
31+
/// 忽略 OPTIONS 请求
32+
/// </summary>
33+
public bool IgnoreOptionsRequest { get; set; } = true;
34+
3035
/// <summary>
3136
/// 是否将捕获到的异常抛出给上层中间件<para/>
3237
/// default is <see langword="false"/>

0 commit comments

Comments
 (0)