【发布时间】:2017-06-28 13:17:57
【问题描述】:
我希望能够注册某些考虑到资源实际 URL 的 URL。具体来说:我的 Web API 文档的 Swagger 端点。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUi(c =>
{
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "API documentation");
//c.SwaggerEndpoint($"/TheGreatestWebAPI/swagger/v1/swagger.json", "API documentation");
});
}
在我的开发环境中,我将在 http://localhost:5000 上托管 Web PI,在这种情况下,Swagger 想在这里与其端点对话:
在生产或暂存环境中,Web API 将作为 Web 应用程序托管在 IIS 中,地址类似于 https://test.contoso.com/TheGreatestWebAPI,并且 swagger 会想与此对话:
https://test.contoso.com/TheGreatestWebAPI/swagger/v1/swagger.json
...因此后者被注释掉的端点注册。
我想做的是使用这样的东西:
c.SwaggerEndpoint($"~/swagger/v1/swagger.json", "API documentation");
使用过 ASP.NET Web 表单Page.ResolveUrl() 的任何人都应该熟悉这一点,据我所知,同样的功能应该可以通过UrlHelper.Content() 获得。但是函数需要UrlHelper类的实例,而相关的构造函数需要ActionContext实例。
我不知道在这种情况下如何正确实例化UrlHelper(Startup.Configure() 中的UseSwaggerUI())。
-S
【问题讨论】:
标签: asp.net-core swagger-ui urlhelper actioncontext