From 4792113f05e5478b83d7b5e3087147a63f03630c Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 6 Aug 2025 14:51:53 +0800 Subject: [PATCH] =?UTF-8?q?Web=EF=BC=9AOPTIONS=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=EF=BC=8C=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Web/ApiProcessor.cs | 11 ++++++++--- Apewer/Web/ApiUtility.cs | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index 7ae4493..5a34d5d 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -124,6 +124,13 @@ namespace Apewer.Web // 寻找入口。 void Invoke() { + // OPTIONS + if (_context.Request.Method == HttpMethod.OPTIONS) + { + _context.Response.Model = new ApiTextModel(""); + return; + } + // 路由 if (_context.Options.UseRoute) { @@ -134,7 +141,6 @@ namespace Apewer.Web { _context.ApiAction = action; Invoke(action); - _context.Response.Duration = Duration(_context.Beginning); return; } } @@ -145,7 +151,6 @@ namespace Apewer.Web var appName = _context.Request.Application; var application = _context.Entries.GetApplication(appName); Invoke(application); - _context.Response.Duration = Duration(_context.Beginning); return; } @@ -580,7 +585,7 @@ namespace Apewer.Web } // Content-Type 检查。 - if (options.WithContentTypeOptions || options.Default != null) + if (options.WithContentTypeOptions) { merged.Add("X-Content-Type-Options", "nosniff"); } diff --git a/Apewer/Web/ApiUtility.cs b/Apewer/Web/ApiUtility.cs index 4e8c8d9..79356d8 100644 --- a/Apewer/Web/ApiUtility.cs +++ b/Apewer/Web/ApiUtility.cs @@ -547,6 +547,15 @@ namespace Apewer.Web Error(context.Response, text); } + /// 渲染为 JSON,并设置到 Response 的 Data 属性。 + public static void SetCamelJsonToResponseData(ApiContext context, object result) + { + if (context == null) return; + if (context.Response == null) return; + if (result == null) return; + context.Response.Data = Json.From(result).Camel(); + } + #endregion #region ApiRequest