优化Swagger文档输出

This commit is contained in:
2025-09-25 15:31:44 +08:00
parent 4a198f1056
commit aa43943f33
4 changed files with 31 additions and 30 deletions

View File

@@ -10,12 +10,12 @@ namespace 电子展板.Models
[Model("返回数据")] [Model("返回数据")]
public class RetValue<T> public class RetValue<T>
{ {
[ModelProperty("返回状态")] [ModelProperty(Description = "返回状态")]
public int state { get; set; } public int state { get; set; }
[ModelProperty("返回消息")] [ModelProperty(Description = "返回消息")]
public string message { get; set; } public string message { get; set; }
[ModelProperty("返回数据")] [ModelProperty(Description = "返回数据")]
public T data { get; set; } public T data { get; set; }
} }
} }

View File

@@ -1,6 +1,8 @@
using Nancy; using Nancy;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Configuration;
using Nancy.Conventions; using Nancy.Conventions;
using Nancy.Json;
using Nancy.Swagger.Annotations; using Nancy.Swagger.Annotations;
using Nancy.Swagger.Services; using Nancy.Swagger.Services;
using Nancy.TinyIoc; using Nancy.TinyIoc;
@@ -25,9 +27,16 @@ namespace 电子展板.WebModule
protected override void ConfigureConventions(NancyConventions nancyConventions) protected override void ConfigureConventions(NancyConventions nancyConventions)
{ {
//... 配置你自己的静态文件等
base.ConfigureConventions(nancyConventions); base.ConfigureConventions(nancyConventions);
} }
public override void Configure(INancyEnvironment environment)
{ //保留属性原始的命名方式,下面两个都得配置,默认都是首字母小写的
SwaggerAnnotationsConfig.RetainCasing = true;
environment.Json(defaultEncoding: System.Text.Encoding.UTF8, retainCasing: true);
base.Configure(environment);
}
/// <summary> /// <summary>
/// 允许跨域 /// 允许跨域

View File

@@ -8,8 +8,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using .Base; using .Base;
using .Models; using .Models;
using .Utility; using .Utility;
@@ -24,23 +22,20 @@ namespace 电子展板.WebModule
{ {
public HomeModule(ISwaggerModelCatalog modelCatalog) public HomeModule(ISwaggerModelCatalog modelCatalog)
{ {
//涉及到哪些Model //涉及到哪些Model
modelCatalog.AddModel<MyConfig>(); modelCatalog.AddModel<MyConfig>();
modelCatalog.AddModel<RetValue<string>>(); modelCatalog.AddModel<RetValue<string>>();
//默认页面 //默认页面
Get("", Index); Get("", _ => Index());
Get("/", Index); Get("/", _ => Index());
Get("/index.html", Index); Get("/index.html", _ => Index());
//全局静态资源 //全局静态资源
Get("/static/{name*}", StaticResource); Get("/static/{name*}", dynamic => StaticResource(dynamic.name ?? ""));
//获得配置 //获得配置
Get("/getConfig", GetConfig, null, "getConfig"); Get("/getConfig", _ => GetConfig(), null, "getConfig");
//获得上传的文件 //获得上传的文件
Get("/upload/{id}", GetUploadFiles); Get("/upload/{id}", dynamic => GetUploadFiles(dynamic.id ?? ""));
//上传图片 //上传图片
Post("/uploadImage", dynamic => Post("/uploadImage", dynamic =>
{ {
@@ -50,7 +45,11 @@ namespace 电子展板.WebModule
return UploadImage(files.FirstOrDefault()); return UploadImage(files.FirstOrDefault());
}, null, "uploadImage"); }, null, "uploadImage");
//保存配置 //保存配置
Post("/save", SaveConfig, null, "save"); Post("/save", dynamic =>
{
MyConfig config = this.Bind<MyConfig>();
return SaveConfig(config);
}, null, "save");
} }
@@ -59,7 +58,7 @@ namespace 电子展板.WebModule
/// </summary> /// </summary>
/// <param name="dynamic"></param> /// <param name="dynamic"></param>
/// <returns></returns> /// <returns></returns>
private object Index(dynamic dynamic) private object Index()
{ {
return Resouce("Views/Index.html"); return Resouce("Views/Index.html");
} }
@@ -70,9 +69,8 @@ namespace 电子展板.WebModule
/// </summary> /// </summary>
/// <param name="dynamic"></param> /// <param name="dynamic"></param>
/// <returns></returns> /// <returns></returns>
private object StaticResource(dynamic dynamic) private object StaticResource(string name)
{ {
string name = dynamic.name;
return Resouce(name); return Resouce(name);
} }
@@ -87,11 +85,9 @@ namespace 电子展板.WebModule
[Route(Summary = "获得配置数据")] [Route(Summary = "获得配置数据")]
[SwaggerResponse(HttpStatusCode.OK, typeof(MyConfig))] [SwaggerResponse(HttpStatusCode.OK, typeof(MyConfig))]
[Route(Tags = new[] { "获得配置数据" })] [Route(Tags = new[] { "获得配置数据" })]
private object GetConfig(dynamic dynamic) private object GetConfig()
{ {
//保留JSON KEY首字母大写这里就不用 Response.AsJson(); return Response.AsJson(GlobalVariable.Config);
//AsJson会将首字母小写
return Response.AsText(JsonHelper.ToJson(GlobalVariable.Config), "application/json", Encoding.UTF8);
} }
@@ -101,9 +97,8 @@ namespace 电子展板.WebModule
/// </summary> /// </summary>
/// <param name="dynamic"></param> /// <param name="dynamic"></param>
/// <returns></returns> /// <returns></returns>
private object GetUploadFiles(dynamic dynamic) private object GetUploadFiles(string id)
{ {
string id = dynamic.id;
string contentType = MimeTypes.GetMimeType(id); string contentType = MimeTypes.GetMimeType(id);
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType); return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
} }
@@ -143,12 +138,8 @@ namespace 电子展板.WebModule
[Route(Summary = "保存配置")] [Route(Summary = "保存配置")]
[SwaggerResponse(HttpStatusCode.OK, typeof(RetValue<string>))] [SwaggerResponse(HttpStatusCode.OK, typeof(RetValue<string>))]
[Route(Tags = new[] { "保存配置" })] [Route(Tags = new[] { "保存配置" })]
private object SaveConfig(dynamic dynamic) private object SaveConfig([RouteParam(ParamIn = ParameterIn.Body, Name = "parms", Description = "保存参数", Required = true)] MyConfig config)
{ {
MyConfig config = this.Bind<MyConfig>();
//string str = Request.Body.AsString();
//MyConfig config = JsonHelper.ToObject<MyConfig>(str);
LogHelper.Instance.Info($"用户保存了配置:{JsonHelper.ToJson(config)}"); LogHelper.Instance.Info($"用户保存了配置:{JsonHelper.ToJson(config)}");
//保存 //保存
PropertyInfo[] propertyInfos = typeof(MyConfig).GetProperties(); PropertyInfo[] propertyInfos = typeof(MyConfig).GetProperties();
@@ -163,6 +154,7 @@ namespace 电子展板.WebModule
RetValue<string> result = new RetValue<string> { state = 1, message = "保存成功" }; RetValue<string> result = new RetValue<string> { state = 1, message = "保存成功" };
return Response.AsJson(result); return Response.AsJson(result);
} }
private object Resouce(string resourcePath) private object Resouce(string resourcePath)
{ {
string packUri = $"pack://application:,,,/Assets/{resourcePath}"; string packUri = $"pack://application:,,,/Assets/{resourcePath}";

View File

@@ -31,7 +31,7 @@
<!-- 日志框架 --> <!-- 日志框架 -->
<PackageReference Include="NLog" Version="6.0.2" /> <PackageReference Include="NLog" Version="6.0.2" />
<!-- WEB 自宿主 --> <!-- WEB 自宿主 -->
<PackageReference Include="ZJW.NancySwagger" Version="2.0.0" /> <PackageReference Include="ZJW.NancySwagger" Version="2.0.1" />
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" /> <PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
<!-- 界面UI--> <!-- 界面UI-->
<PackageReference Include="HandyControl" Version="3.5.1" /> <PackageReference Include="HandyControl" Version="3.5.1" />