Search code examples
asp.net.netasp.net-core.net-coreswagger

asp.net core swagger RoutePrefix


i had a api site use swagger to expose api. dotnet core version is 2.1 and swagger is Swashbuckle.AspNetCore 3.0.0. my api site is behind a reverse proxy(nginx), the nginx url is https://www.aa.com/chat, map to backen server http://127.0.0.1:5003/ and my swagger config is : ConfigureServices:

services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Info { Title = "aa.IM.CustomerApi", Version = "v1" });
                    var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                    var controllerXmlPath = Path.Combine(basePath, "aa.IM.CustomerApi.xml");
                    var entityXmlPath = Path.Combine(basePath, "aa.IM.Entity.xml");
                    c.IncludeXmlComments(controllerXmlPath);
                    c.IncludeXmlComments(entityXmlPath);
                    //c.DescribeAllEnumsAsStrings();
                });

Configure:

app.UseSwagger(c =>
            {
            });
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("v1/swagger.json", "aa.IM.CustomerApi");
            });

then, i can enter into the ui https://www.aa.com/chat/swagger/index.html and then ui can dispaly all perfect, but, when i execute a api (the uri is cus/settings), i found the request url https://www.aa.com/cus/settings is wrong, the correct url is https://www.aa.com/chat/cus/settings.

how can i fix my swagger config to resole the problem? i had try many way, like http://eatcodelive.com/2017/05/19/change-default-swagger-route-in-an-asp-net-core-web-api/ and How to change base url of Swagger in ASP.NET core but these all can not resolve my problem.


Solution

  • thanks all, i resolved it by below config:

                app.UseSwagger(c =>
                {
                    c.PreSerializeFilters.Add((doc, requset) =>
                    {
                        var root = "https://www.aa.com/chat/";
                        doc.Host = root;
                    });
                });