新增SwaggerDemo
This commit is contained in:
18
电子展板/Utility/Swagger/SwaggerClassCommentAttribute.cs
Normal file
18
电子展板/Utility/Swagger/SwaggerClassCommentAttribute.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 电子展板.Utility.Swagger
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class SwaggerClassCommentAttribute : Attribute
|
||||
{
|
||||
public string Comment { get; set; }
|
||||
public SwaggerClassCommentAttribute(string comment)
|
||||
{
|
||||
Comment = comment;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
电子展板/Utility/Swagger/SwaggerCommentAttribute.cs
Normal file
39
电子展板/Utility/Swagger/SwaggerCommentAttribute.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 电子展板.Utility.Swagger
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class SwaggerCommentAttribute : Attribute
|
||||
{
|
||||
public string Comment { get; set; }
|
||||
public bool? IsRequired { get; set; }
|
||||
|
||||
public long? Min { get; set; }
|
||||
public long? Max { get; set; }
|
||||
public SwaggerCommentAttribute()
|
||||
{
|
||||
Comment = "";
|
||||
}
|
||||
public SwaggerCommentAttribute(string comment)
|
||||
{
|
||||
Comment = comment;
|
||||
}
|
||||
public SwaggerCommentAttribute(string comment, bool isRequired)
|
||||
{
|
||||
Comment = comment;
|
||||
IsRequired = isRequired;
|
||||
}
|
||||
public SwaggerCommentAttribute(string comment, bool isRequired, long min, long max)
|
||||
{
|
||||
Comment = comment;
|
||||
IsRequired = isRequired;
|
||||
Min = min;
|
||||
Max = max;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
41
电子展板/Utility/Swagger/SwaggerModelDataUtils.cs
Normal file
41
电子展板/Utility/Swagger/SwaggerModelDataUtils.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Nancy.Swagger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 电子展板.Utility.Swagger
|
||||
{
|
||||
public static class SwaggerModelDataUtils
|
||||
{
|
||||
public static SwaggerModelData GetModelData<T>()
|
||||
{
|
||||
return SwaggerModelData.ForType<T>(with =>
|
||||
{
|
||||
Type type = typeof(T);
|
||||
SwaggerClassCommentAttribute classComment = type.GetCustomAttribute<SwaggerClassCommentAttribute>();
|
||||
with.Description(classComment != null ? classComment.Comment : type.Name);
|
||||
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
|
||||
foreach (PropertyInfo propertyInfo in propertyInfos)
|
||||
{
|
||||
SwaggerModelPropertyData ss = with.Data.Properties.Where(x => x.Name == propertyInfo.Name).FirstOrDefault();
|
||||
SwaggerCommentAttribute attribute = propertyInfo.GetCustomAttribute<SwaggerCommentAttribute>();
|
||||
if (attribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(attribute.Comment))
|
||||
ss.Description = attribute.Comment;
|
||||
if (attribute.IsRequired != null)
|
||||
ss.Required = attribute.IsRequired.Value;
|
||||
if (attribute.Min != null)
|
||||
ss.Minimum = attribute.Min.Value;
|
||||
if (attribute.Max != null)
|
||||
ss.Maximum = attribute.Max.Value;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
56
电子展板/Utility/Swagger/SwaggerModule.cs
Normal file
56
电子展板/Utility/Swagger/SwaggerModule.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Nancy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 电子展板.Utility.Swagger
|
||||
{
|
||||
public class SwaggerModule : NancyModule
|
||||
{
|
||||
public SwaggerModule()
|
||||
{
|
||||
//全局静态资源
|
||||
Get("/swagger", RedirectToIndex);
|
||||
Get("/swagger/index.html", Index);
|
||||
Get("/swagger/{name*}", StaticResource);
|
||||
}
|
||||
|
||||
private object RedirectToIndex(dynamic dynamic)
|
||||
{
|
||||
return Response.AsRedirect("/swagger/index.html");
|
||||
}
|
||||
|
||||
private object Index(dynamic dynamic)
|
||||
{
|
||||
var url = $"{Request.Url.BasePath}/api-docs";
|
||||
string packUri = $"pack://application:,,,/Assets/SwaggerUI/index.html";
|
||||
string contentType = System.Web.MimeMapping.GetMimeMapping(packUri);
|
||||
byte[] bytes = null;
|
||||
using (var stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream)
|
||||
{
|
||||
bytes = new byte[stream.Length];
|
||||
stream.Read(bytes, 0, bytes.Length);
|
||||
}
|
||||
string html = Encoding.UTF8.GetString(bytes);
|
||||
html = html.Replace("@url", url);
|
||||
return Response.AsText(html, contentType);
|
||||
}
|
||||
|
||||
private object StaticResource(dynamic dynamic)
|
||||
{
|
||||
string name = dynamic.name;
|
||||
return Resouce(name);
|
||||
}
|
||||
|
||||
private object Resouce(string resourcePath)
|
||||
{
|
||||
if (resourcePath == "")
|
||||
resourcePath = "index.html";
|
||||
string packUri = $"pack://application:,,,/Assets/SwaggerUI/{resourcePath}";
|
||||
string contentType = System.Web.MimeMapping.GetMimeMapping(packUri);
|
||||
return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user