简化使用

This commit is contained in:
2025-09-24 12:02:13 +08:00
parent dad5b8fb89
commit 4a198f1056
13 changed files with 103 additions and 375 deletions

View File

@@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace .Utility
{
public class ContentTypeUtils
{
public static string GetContentType(string fileName)
{
return System.Web.MimeMapping.GetMimeMapping(fileName);
}
}
}

View File

@@ -1,18 +0,0 @@
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;
}
}
}

View File

@@ -1,39 +0,0 @@
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;
}
}
}

View File

@@ -1,41 +0,0 @@
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;
}
});
}
}
}

View File

@@ -1,56 +0,0 @@
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);
}
}
}