hemiaoio / hamazon.swagger.ui Goto Github PK
View Code? Open in Web Editor NEWC# Code from Swashbuckle.AspNetCore.SwaggerUI . Ui Code from https://github.com/jensoleg/swagger-ui
License: Apache License 2.0
C# Code from Swashbuckle.AspNetCore.SwaggerUI . Ui Code from https://github.com/jensoleg/swagger-ui
License: Apache License 2.0
方便留个qq咨询点问题么?
我的是18-33-168-29
因为.net mvc 的Controller 都是有 Controller 后缀的,但是swagger.json并没有这个Controller后缀,而 Swashbuckle.AspNetCore 官方作者也没有做这个转换(也有可能涉及到自定义后缀),目前在这里自行处理记录一下:
using System;
using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Xml.XPath;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
namespace xxxxxx
{
/// <summary>
///
/// </summary>
public class SwaggerDocumentFilter : IDocumentFilter
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IEnumerable<XPathDocument> _xmlDocuments;
/// <summary>
///
/// </summary>
/// <param name="xmlDocuments"></param>
/// <param name="httpContextAccessor"></param>
public SwaggerDocumentFilter(IEnumerable<XPathDocument> xmlDocuments, IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_xmlDocuments = xmlDocuments;
}
/// <summary>
///
/// </summary>
/// <param name="swaggerDoc"></param>
/// <param name="context"></param>
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.BasePath = "/";
swaggerDoc.Schemes = new List<string> { _httpContextAccessor.HttpContext.Request.Scheme };
swaggerDoc.Host = _httpContextAccessor.HttpContext.Request.Host.Value;
//增加Controller说明信息
swaggerDoc.Tags = new List<Tag>();
foreach (var descriptionGroup in context.ApiDescriptionsGroups.Items)
{
var actionDescriptor = descriptionGroup.Items.First().ActionDescriptor;
if (actionDescriptor is ControllerActionDescriptor)
{
var controllerActionDescriptor = actionDescriptor as ControllerActionDescriptor;
var type = controllerActionDescriptor.ControllerTypeInfo.UnderlyingSystemType;
swaggerDoc.Tags.Add(new Tag
{
Name = descriptionGroup.GroupName,
Description = GetSummaryByType(type)
});
}
}
}
private const string MemberXPath = "/doc/members/member[@name='{0}']";
private const string SummaryTag = "summary";
/// <summary>
/// 根据类型ID获取XML备注
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private string GetSummaryByType(Type type)
{
var commentId = XmlCommentsIdHelper.GetCommentIdForType(type);
foreach (var document in _xmlDocuments)
{
var xmlNavigator = document.CreateNavigator();
var typeNode = xmlNavigator.SelectSingleNode(string.Format(MemberXPath, commentId));
if (typeNode != null)
{
var summaryNode = typeNode.SelectSingleNode(SummaryTag);
if (summaryNode != null)
return XmlCommentsTextHelper.Humanize(summaryNode.InnerXml);
}
}
return string.Empty;
}
}
}
public IServiceProvider ConfigureServices(IServiceCollection services){
/******Your Self Services Register.........******/
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info
{
Title = "Swagger API Document",
Version = "v1",
Contact = new Contact()
{
Email = "[email protected]",
Name = "Hemiao"
},
License = new License()
{
Name = "Apache 2.0",
Url = "http://www.apache.org/licenses/LICENSE-2.0.html"
}
});
options.AddSecurityDefinition(string.Empty, new BasicAuthScheme() { });
options.DocInclusionPredicate((docName, description) => true);
options.DescribeAllEnumsAsStrings();
//取本项目相关的XML地址,如有其他需要添加的XML需要在这里进行修改
var xmlFilePaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,
Assembly.GetExecutingAssembly().GetName().Name
.Substring(0, Assembly.GetExecutingAssembly().GetName().Name.IndexOf('.')) +
".*.xml",
SearchOption.AllDirectories);
//将XML转成 XPathDocument
var documents = xmlFilePaths.Select(path => new XPathDocument(path));
//用于取Controller的说明
options.DocumentFilter<SwaggerDocumentFilter>(documents);
foreach (var document in documents)
{
options.IncludeXmlComments(() => document);
}
});
/******Your Self Services Register.........******/
}
使用360极速浏览器时,样式打乱,只支持IE浏览器吗?
Swashbuckle.AspNetCore.Swagger的版本是1.2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.