【发布时间】:2018-03-01 14:42:32
【问题描述】:
我在使用自定义 index.html 和其他带有 swashbuckle 的资产时遇到了困难。 Swashbuckle/Swagger 似乎根本不认识或使用它们。我确实有 app.UseDefaultFiles() 和 app.UseStaticFiles() 设置。我试图了解我做错了什么。
我试图设置我的配置,有点类似于 Microsoft 文章中定义的配置,但没有成功。 (https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio)
我目前正在使用文章 (https://github.com/swagger-api/swagger-ui/tree/2.x/dist) 中引用的 dist 文件夹中的文件以及提供的自定义 css 文件。
我的 index.html 文件位于 /wwwroot/swagger/ui 下 自定义 css 文件位于 /wwwroot/swagger/ui/css 下(作为 custom.css)
这是我的 Startup.cs 类。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options =>
{
// Swagger - Format JSON
options.SerializerSettings.Formatting = Formatting.Indented;
});
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.DescribeAllEnumsAsStrings();
c.DescribeStringEnumsInCamelCase();
// c.DescribeAllParametersInCamelCase();
c.SwaggerDoc("v1",
new Info
{
Title = "My Web API - v1",
Version = "v1",
Description = "New and improved version. A simple example ASP.NET Core Web API. "
}
);
c.SwaggerDoc("v2",
new Info
{
Title = "My Web API - v2",
Version = "v2",
Description = "New and improved version. A simple example ASP.NET Core Web API. "
}
);
// Set the comments path for the Swagger JSON and UI.
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "ApiTest.xml");
c.IncludeXmlComments(xmlPath);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
string swaggerUIFilesPath = env.WebRootPath + "\\swagger\\ui";
if (!string.IsNullOrEmpty(swaggerUIFilesPath))
{
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(swaggerUIFilesPath),
RequestPath = new PathString("/api-docs"),
});
}
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c =>
{
c.RouteTemplate = "api-docs/{documentName}/swagger.json";
});
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
//c.ShowJsonEditor();
c.RoutePrefix = "api-docs";
c.SwaggerEndpoint("/api-docs/v1/swagger.json", "My Web API - V1 ");
c.SwaggerEndpoint("/api-docs/v2/swagger.json", "My Web API - V2 ");
c.DocumentTitle("My Web API");
});
app.UseMvc();
}
}
我的最终目标是能够使用类似于此处可用的石板风格主题 (https://github.com/omnifone/slate-swagger-ui)。目前,我只是想让 Swashbuckle/Swagger 使用 Microsoft 文档中引用的自定义文件,然后再尝试使其他文件正常工作。
我真的不想尝试将我的资产转换为嵌入式资源——因为其中有很多。我只想引用一个普通的 index.html 文件并能够使用它所有引用的文件。
我做错了什么?
相关软件版本
- .Net Core 版本:2.0.3
- Swashbuckle.AspNetCore:1.2.0
- Windows 10 企业版 1703
- Visual Studio 2017 企业版 15.5.2
【问题讨论】:
-
请忽略上述问题。没有问题。有时在向 Stack Overflow 提交问题之前,在 Chrome 中按 Ctrl-F5 以清除缓存很有用。对于那些可能需要在 ASP.NET Core 2.x 中使用带有 Swashbuckle 的 Index.Html 的人,上面的代码 sn-p 应该可以正常工作。
标签: swagger asp.net-core-2.0 swagger-2.0 swashbuckle