首页 > c# > 如何阻止Swashbuckle添加不需要的(和未指定的)基本URL?

如何阻止Swashbuckle添加不需要的(和未指定的)基本URL? (How can I stop Swashbuckle adding an unwanted (and unspecified) Base URL?)

问题

我有一个奇怪的问题,我的API项目已开始插入一个基本URL(“/ swagger”)似乎一夜之间没有更改代码。基本URL仅在我在本地处理项目时出现,当它部署到Azure应用程序服务时,一切正常。有关问题,请参见图片:

在此输入图像描述

在启动时,我的swagger设置如下所示:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My AppName API", Version = "v1" });
        c.OperationFilter<SwaggerTokenParameter>();
    });
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("../swagger/v1/swagger.json", "V1 Docs");
    });

    ...            
}

我已经尝试将项目重新克隆到我的机器上,但问题仍然存在,我还尝试手动设置RoutePrefix

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("../swagger/v1/swagger.json", "V1 Docs");
    c.RoutePrefix = string.Empty;
}

这没有用 - 它只是打破了UI,定义仍然包含基本URL:

{"swagger":"2.0","info":{"version":"v1","title":"My AppName API"},"basePath":"/swagger",...

解决方法

由于这个答案,我能够提出一个解决方案。我仍然不明白在基本URL中添加了哪些内容,或者为什么它不会在App Service中发生。

app.UseSwagger(c =>
{
    c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
    {
        swaggerDoc.BasePath = null;
    });
});

问题

I have an odd problem where my API project has started inserting a base URL ("/swagger") seemingly overnight with no changes to the code. The base URL only appears when I am working on the project locally, when it is deployed to an Azure app service everything is normal. See image for the problem:

enter image description here

In startup my swagger setup looks like this:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My AppName API", Version = "v1" });
        c.OperationFilter<SwaggerTokenParameter>();
    });
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("../swagger/v1/swagger.json", "V1 Docs");
    });

    ...            
}

I have tried recloning the project onto my machine but the problem has persisted, I have also tried manually setting the RoutePrefix:

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("../swagger/v1/swagger.json", "V1 Docs");
    c.RoutePrefix = string.Empty;
}

This doesn't help - it just breaks the UI and the definition still contains the base url:

{"swagger":"2.0","info":{"version":"v1","title":"My AppName API"},"basePath":"/swagger",...

解决方法

Thanks to this answer I was able to come up with a solution. I still don't understand what changed to add in the base URL though or why it doesn't happen in the App Service.

app.UseSwagger(c =>
{
    c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
    {
        swaggerDoc.BasePath = null;
    });
});
相似信息