From 78389519e546d5b366cea83f67df4016a3f6a290 Mon Sep 17 00:00:00 2001
From: chenhaozhe <gaozhechen26@gmail.com>
Date: 星期二, 10 十一月 2026 21:28:29 +0800
Subject: [PATCH] 网关添加 跨域配置
---
appsettings.json | 6 +++++-
Properties/PublishProfiles/FolderProfile.pubxml.user | 2 +-
Program.cs | 12 ++++++++++++
APIGetway.csproj.user | 2 +-
4 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/APIGetway.csproj.user b/APIGetway.csproj.user
index 8c05e64..0fee2b9 100644
--- a/APIGetway.csproj.user
+++ b/APIGetway.csproj.user
@@ -2,7 +2,7 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>http</ActiveDebugProfile>
- <NameOfLastUsedPublishProfile>F:\WorkBench\APIGetway\Properties\PublishProfiles\FolderProfile1.pubxml</NameOfLastUsedPublishProfile>
+ <NameOfLastUsedPublishProfile>F:\ProjFolder\APIGetway\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
diff --git a/Program.cs b/Program.cs
index e16352d..3db8134 100644
--- a/Program.cs
+++ b/Program.cs
@@ -10,6 +10,14 @@
builder.Services.AddWindowsService(options => options.ServiceName = "APIGetway");
+builder.Services.AddCors(options =>
+{
+ options.AddPolicy("frontend-dev", policy => policy
+ .WithOrigins("http://192.168.0.102:32976") // 鎹㈡垚鍓嶇鐪熷疄鍦板潃+绔彛
+ .WithMethods("GET", "POST", "PUT", "DELETE")
+ .WithHeaders("Content-Type", "X-Username", "Authorization"));
+});
+
// YARP 鍙嶅悜浠g悊
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
@@ -24,6 +32,10 @@
app.UseDefaultFiles();
app.UseStaticFiles();
+// 璺ㄥ煙涓棿浠讹細YARP 璺敱甯� CorsPolicy 鏃朵細寰�绔偣鍐� CORS 鍏冩暟鎹紝
+// 缂轰簡 UseCors 灏变細鍦ㄨ姹傛椂鎶� "contains CORS metadata, but a middleware was not found"
+app.UseCors();
+
// YARP 杞彂锛氭寜 appsettings.json 涓殑 Routes 瑙勫垯杞彂鍒板悗绔�
app.MapReverseProxy();
diff --git a/Properties/PublishProfiles/FolderProfile.pubxml.user b/Properties/PublishProfiles/FolderProfile.pubxml.user
index 02b24c9..7c7a4fc 100644
--- a/Properties/PublishProfiles/FolderProfile.pubxml.user
+++ b/Properties/PublishProfiles/FolderProfile.pubxml.user
@@ -3,7 +3,7 @@
<Project>
<PropertyGroup>
<_PublishTargetUrl>D:\缃戠珯鍙戝竷\鏅轰簯MESWMS\APIGetway</_PublishTargetUrl>
- <History>False|2026-11-09T01:48:19.1650763Z||;False|2026-11-09T09:48:16.4210302+08:00||;True|2026-11-05T15:24:01.6424731+08:00||;</History>
+ <History>True|2026-11-10T13:20:22.6009380Z||;False|2026-11-10T21:20:15.2216854+08:00||;True|2026-11-10T21:17:36.8635448+08:00||;True|2026-11-10T21:17:05.2126541+08:00||;True|2026-11-10T21:16:52.7350060+08:00||;True|2026-11-10T21:08:45.9397745+08:00||;True|2026-11-10T21:03:17.7382868+08:00||;False|2026-11-09T09:48:19.1650763+08:00||;False|2026-11-09T09:48:16.4210302+08:00||;True|2026-11-05T15:24:01.6424731+08:00||;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>
\ No newline at end of file
diff --git a/appsettings.json b/appsettings.json
index a13e719..e75c39a 100644
--- a/appsettings.json
+++ b/appsettings.json
@@ -21,6 +21,7 @@
"Routes": {
"mes-backend": {
"ClusterId": "mes-backend",
+ "CorsPolicy": "frontend-dev",
"Match": {
"Path": "/API/{**catch-all}"
},
@@ -38,5 +39,8 @@
}
}
}
+ // 娉ㄦ剰锛歒ARP 娌℃湁 "CorsPolicies" 杩欎釜閰嶇疆鑺傦紙鍐欎簡涔熶細琚潤榛樺拷鐣ワ級銆�
+ // 璺敱涓婄殑 "CorsPolicy" 鍙槸绛栫暐鍚嶏紝鐪熸鐨勭瓥鐣ュ畾涔夊湪 Program.cs 鐨� AddCors(...) 閲岋紝
+ // 涓ゅ鍚嶅瓧蹇呴』瀹屽叏涓�鑷淬��
}
-}
\ No newline at end of file
+}
--
Gitblit v1.9.1