From 8dbcb6146478ca2be40e4afdcdba87df81c092ac Mon Sep 17 00:00:00 2001 From: milimoe Date: Sun, 19 Jan 2025 01:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Open=20API=20=E5=92=8C=20S?= =?UTF-8?q?calar=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.WebAPI/FunGame.WebAPI.csproj | 3 +- FunGame.WebAPI/Program.cs | 37 +++---------------- FunGame.WebAPI/Properties/launchSettings.json | 6 +-- 3 files changed, 10 insertions(+), 36 deletions(-) diff --git a/FunGame.WebAPI/FunGame.WebAPI.csproj b/FunGame.WebAPI/FunGame.WebAPI.csproj index 133c537..f3088b1 100644 --- a/FunGame.WebAPI/FunGame.WebAPI.csproj +++ b/FunGame.WebAPI/FunGame.WebAPI.csproj @@ -25,7 +25,8 @@ - + + diff --git a/FunGame.WebAPI/Program.cs b/FunGame.WebAPI/Program.cs index 2ab8916..242786f 100644 --- a/FunGame.WebAPI/Program.cs +++ b/FunGame.WebAPI/Program.cs @@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.Logging.Console; using Microsoft.IdentityModel.Tokens; -using Microsoft.OpenApi.Models; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Library.Common.Addon; using Milimoe.FunGame.Core.Library.Common.Network; @@ -21,6 +20,7 @@ using Milimoe.FunGame.Server.Others; using Milimoe.FunGame.Server.Utility; using Milimoe.FunGame.WebAPI.Architecture; using Milimoe.FunGame.WebAPI.Services; +using Scalar.AspNetCore; WebAPIListener listener = new(); @@ -115,40 +115,13 @@ try }); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(options => - { - options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme - { - Name = "Authorization", - Type = SecuritySchemeType.Http, - Scheme = "Bearer", - BearerFormat = "JWT", - In = ParameterLocation.Header, - Description = "输入 Auth 返回的 BearerToken", - }); - options.AddSecurityRequirement(new OpenApiSecurityRequirement - { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "Bearer" - } - }, - Array.Empty() - } - }); - }); + builder.Services.AddOpenApi(); // 添加 CORS 服务 builder.Services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", policy => { - policy.AllowAnyOrigin() - .AllowAnyHeader() - .AllowAnyMethod(); + policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); }); }); // 添加 JWT 认证 @@ -185,8 +158,8 @@ try // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.MapOpenApi(); + app.MapScalarApiReference(); } app.UseDefaultFiles(); diff --git a/FunGame.WebAPI/Properties/launchSettings.json b/FunGame.WebAPI/Properties/launchSettings.json index 5c47465..88622e2 100644 --- a/FunGame.WebAPI/Properties/launchSettings.json +++ b/FunGame.WebAPI/Properties/launchSettings.json @@ -13,7 +13,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar", "applicationUrl": "http://localhost:5117", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" @@ -23,7 +23,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar", "applicationUrl": "https://localhost:7162;http://localhost:5117", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" @@ -32,7 +32,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }