Ant*_*lin 3 swagger swagger-2.0 swashbuckle asp.net-core-2.0
我在使用自定义index.html和其他资产时遇到了困难。Swashbuckle / Swagger似乎根本无法识别或使用它们。我确实设置了app.UseDefaultFiles()和app.UseStaticFiles()。我试图了解我做错了什么。
我试图设置与Microsoft文章中定义的配置有些类似的配置,但没有成功。(https://docs.microsoft.com/zh-cn/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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是能够使用类似此处的slate样式主题的东西(https://github.com/omnifone/slate-swagger-ui)。现在,我只是试图让Swashbuckle / Swagger使用Microsoft文档中引用的自定义文件,然后再尝试使其他文件正常工作。
我真的不想尝试将我的资产转换为嵌入式资源-因为会有很多资源。我只想引用一个普通的index.html文件,并能够使用其所有引用的文件。
我究竟做错了什么?
相关软件版本
这是我发现在.NET Core项目中替换SwashBuckle的index.html所需采取的最少措施:
从此处获取原始index.html的副本:https : //github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerUI/index.html
将该副本放在项目的某些子文件夹中。
我选择的文件名称可能不同:
\Resources\Swagger_Custom_index.html
在解决方案资源管理器中右键单击该文件,选择“属性”,在左窗格中选择“配置属性”。在右窗格的“高级”下,找到“构建操作”条目并将其设置为“嵌入式资源”。单击确定。
在Startup.cs中,将以下行添加到您的app.UseSwaggerUI()呼叫中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//...
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Your.Default.Namespace.Resources.Swagger_Custom_index.html");
});
//...
}
Run Code Online (Sandbox Code Playgroud)上述GetManifestResourceStream方法中文件资源的标识包括:
这三个部分都用点连接起来(此处没有斜杠或反斜杠)。
如果您不使用子路径,但将资源放在根目录下,则只需省略第2部分。
| 归档时间: |
|
| 查看次数: |
4050 次 |
| 最近记录: |