[英]ASP.NET Core resolve url in startup?
我希望能夠注冊考慮到資源的實際網址的某些網址。 具體來說:我的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 Page.ResolveUrl()
,從我可以看到,通過UrlHelper.Content()
可以獲得相同的功能。 但是,該函數需要UrlHelper類的實例,相關的構造函數需要ActionContext實例。
關於如何在此上下文中正確實例化UrlHelper
UseSwaggerUI()
Startup.Configure()
的UseSwaggerUI()
,我感到很UseSwaggerUI()
。
-S
該方法的文檔說明路徑可以是完全限定的或相對於頁面。
在我的情況下,只需刪除路徑的第一部分就足夠了:
c.SwaggerEndpoint("v1/swagger.json", "API documentation");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.