app.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpReq) => { string swagger_index_url = httpReq.Headers["Referer"]; var index = swagger_index_url.IndexOf("/swagger/index.html", 0, StringComparison.OrdinalIgnoreCase); var swagger_server_prefix = httpReq.Host.Value; if (index > 0) { swagger_server_prefix = swagger_index_url.Substring(0, index + 1); } swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = swagger_server_prefix } }; }); }); app.UseSwaggerUI(c => { //使用相对路径 c.SwaggerEndpoint("v1/swagger.json", "API"); });