diff --git a/24Hour/24Hour.csproj b/24Hour/24Hour.csproj
index a8d24c6..b5c1b97 100644
--- a/24Hour/24Hour.csproj
+++ b/24Hour/24Hour.csproj
@@ -5,10 +5,11 @@
enable
enable
_24Hour
+ True
- E:\Code\24Hour.Service\24Hour\24Hour.xml
+ E:\Code\24Hour.Service\24Hour\bin\Debug\net6.0\24Hour.xml
diff --git a/24Hour/Controllers/LoginController.cs b/24Hour/Controllers/LoginController.cs
index ffc84a4..8f3de09 100644
--- a/24Hour/Controllers/LoginController.cs
+++ b/24Hour/Controllers/LoginController.cs
@@ -1,4 +1,5 @@
using Elight.Entity;
+using Elight.Utility;
using Elight.Utility.Code;
using Elight.Utility.Encrypt;
using Microsoft.AspNetCore.Mvc;
@@ -16,6 +17,7 @@ namespace _24Hour.Controllers
///
/// 登录
///
+ [HiddenApi]
[ApiController]
[Route("api/APP")]
public class LoginController : ControllerBase
diff --git a/24Hour/Program.cs b/24Hour/Program.cs
index 53b4e84..45217eb 100644
--- a/24Hour/Program.cs
+++ b/24Hour/Program.cs
@@ -22,27 +22,18 @@ var Configuration = builder.Configuration;
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
+#region
+#endregion
builder.Services.AddSwaggerGen(c =>
{
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "24小时一体机 API", Version = "v1" });
- c.CustomOperationIds(apiDesc =>
- {
- var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
- return controllerAction?.ControllerName + "-" + controllerAction?.ActionName;
- });
-
- c.ResolveConflictingActions(apiDescription => apiDescription.First());
- c.CustomSchemaIds(x => x.FullName);
- c.DocInclusionPredicate((docName, description) => true);
-
- var xmlFiles = Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "xml"), "*.xml");
- if (xmlFiles.Length > 0)
- {
- foreach (var xmlFile in xmlFiles)
- {
- c.IncludeXmlComments(xmlFile, true);
- }
- }
+ c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
+ //在接口类、方法标记属性 [HiddenApi],可以阻止【Swagger文档】生成
+ c.DocumentFilter();
+ c.OrderActionsBy(o => o.RelativePath);
+ var basePath = System.AppDomain.CurrentDomain.BaseDirectory;//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
+ var xmlPath = Path.Combine(basePath, "24Hour.xml");
+ if (File.Exists(xmlPath))//避免没有该文件时报错
+ c.IncludeXmlComments(xmlPath, true);
//添加Jwt验证设置
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
@@ -67,6 +58,10 @@ builder.Services.AddSwaggerGen(c =>
Type = SecuritySchemeType.ApiKey
});
});
+builder.Services.AddControllers().AddJsonOptions(options =>
+{
+ options.JsonSerializerOptions.PropertyNamingPolicy = null;
+});
// 添加身份验证服务
builder.Services.AddAuthentication(options =>
{
diff --git a/Elight.Utility/Elight.Utility.csproj b/Elight.Utility/Elight.Utility.csproj
index b491c85..afd13e6 100644
--- a/Elight.Utility/Elight.Utility.csproj
+++ b/Elight.Utility/Elight.Utility.csproj
@@ -12,6 +12,7 @@
+
diff --git a/Elight.Utility/HiddenApiFilter.cs b/Elight.Utility/HiddenApiFilter.cs
new file mode 100644
index 0000000..58288b8
--- /dev/null
+++ b/Elight.Utility/HiddenApiFilter.cs
@@ -0,0 +1,41 @@
+锘縰sing Microsoft.AspNetCore.Mvc.ApiExplorer;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Elight.Utility
+{
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
+ public class HiddenApiAttribute : System.Attribute
+ {
+ }
+
+ public class HiddenApiFilter : IDocumentFilter
+ {
+ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
+ {
+ foreach (ApiDescription apiDescription in context.ApiDescriptions)
+ {
+ if (apiDescription.TryGetMethodInfo(out MethodInfo method))
+ {
+ if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute))
+ || method.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute)))
+ {
+ string key = "/" + apiDescription.RelativePath;
+ if (key.Contains("?"))
+ {
+ int idx = key.IndexOf("?", System.StringComparison.Ordinal);
+ key = key.Substring(0, idx);
+ }
+ swaggerDoc.Paths.Remove(key);
+ }
+ }
+ }
+ }
+ }
+}