优化Swagger文档输出
This commit is contained in:
@@ -10,12 +10,12 @@ namespace 电子展板.Models
|
||||
[Model("返回数据")]
|
||||
public class RetValue<T>
|
||||
{
|
||||
[ModelProperty("返回状态")]
|
||||
[ModelProperty(Description = "返回状态")]
|
||||
public int state { get; set; }
|
||||
[ModelProperty("返回消息")]
|
||||
[ModelProperty(Description = "返回消息")]
|
||||
public string message { get; set; }
|
||||
|
||||
[ModelProperty("返回数据")]
|
||||
[ModelProperty(Description = "返回数据")]
|
||||
public T data { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Nancy;
|
||||
using Nancy.Bootstrapper;
|
||||
using Nancy.Configuration;
|
||||
using Nancy.Conventions;
|
||||
using Nancy.Json;
|
||||
using Nancy.Swagger.Annotations;
|
||||
using Nancy.Swagger.Services;
|
||||
using Nancy.TinyIoc;
|
||||
@@ -25,9 +27,16 @@ namespace 电子展板.WebModule
|
||||
|
||||
protected override void ConfigureConventions(NancyConventions 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>
|
||||
/// 允许跨域
|
||||
|
||||
@@ -8,8 +8,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using 电子展板.Base;
|
||||
using 电子展板.Models;
|
||||
using 电子展板.Utility;
|
||||
@@ -24,23 +22,20 @@ namespace 电子展板.WebModule
|
||||
{
|
||||
public HomeModule(ISwaggerModelCatalog modelCatalog)
|
||||
{
|
||||
|
||||
//涉及到哪些Model
|
||||
modelCatalog.AddModel<MyConfig>();
|
||||
modelCatalog.AddModel<RetValue<string>>();
|
||||
|
||||
|
||||
//默认页面
|
||||
Get("", Index);
|
||||
Get("/", Index);
|
||||
Get("/index.html", Index);
|
||||
Get("", _ => Index());
|
||||
Get("/", _ => 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 =>
|
||||
{
|
||||
@@ -50,7 +45,11 @@ namespace 电子展板.WebModule
|
||||
return UploadImage(files.FirstOrDefault());
|
||||
}, 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>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <returns></returns>
|
||||
private object Index(dynamic dynamic)
|
||||
private object Index()
|
||||
{
|
||||
return Resouce("Views/Index.html");
|
||||
}
|
||||
@@ -70,9 +69,8 @@ namespace 电子展板.WebModule
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <returns></returns>
|
||||
private object StaticResource(dynamic dynamic)
|
||||
private object StaticResource(string name)
|
||||
{
|
||||
string name = dynamic.name;
|
||||
return Resouce(name);
|
||||
}
|
||||
|
||||
@@ -87,11 +85,9 @@ namespace 电子展板.WebModule
|
||||
[Route(Summary = "获得配置数据")]
|
||||
[SwaggerResponse(HttpStatusCode.OK, typeof(MyConfig))]
|
||||
[Route(Tags = new[] { "获得配置数据" })]
|
||||
private object GetConfig(dynamic dynamic)
|
||||
private object GetConfig()
|
||||
{
|
||||
//保留JSON KEY首字母大写,这里就不用 Response.AsJson();
|
||||
//AsJson会将首字母小写
|
||||
return Response.AsText(JsonHelper.ToJson(GlobalVariable.Config), "application/json", Encoding.UTF8);
|
||||
return Response.AsJson(GlobalVariable.Config);
|
||||
}
|
||||
|
||||
|
||||
@@ -101,9 +97,8 @@ namespace 电子展板.WebModule
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <returns></returns>
|
||||
private object GetUploadFiles(dynamic dynamic)
|
||||
private object GetUploadFiles(string id)
|
||||
{
|
||||
string id = dynamic.id;
|
||||
string contentType = MimeTypes.GetMimeType(id);
|
||||
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
|
||||
}
|
||||
@@ -143,12 +138,8 @@ namespace 电子展板.WebModule
|
||||
[Route(Summary = "保存配置")]
|
||||
[SwaggerResponse(HttpStatusCode.OK, typeof(RetValue<string>))]
|
||||
[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)}");
|
||||
//保存
|
||||
PropertyInfo[] propertyInfos = typeof(MyConfig).GetProperties();
|
||||
@@ -163,6 +154,7 @@ namespace 电子展板.WebModule
|
||||
RetValue<string> result = new RetValue<string> { state = 1, message = "保存成功" };
|
||||
return Response.AsJson(result);
|
||||
}
|
||||
|
||||
private object Resouce(string resourcePath)
|
||||
{
|
||||
string packUri = $"pack://application:,,,/Assets/{resourcePath}";
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<!-- 日志框架 -->
|
||||
<PackageReference Include="NLog" Version="6.0.2" />
|
||||
<!-- 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" />
|
||||
<!-- 界面UI-->
|
||||
<PackageReference Include="HandyControl" Version="3.5.1" />
|
||||
|
||||
Reference in New Issue
Block a user