3

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.

mofee
  • 135
  • 1
  • 9
  • in dot net framework version, this can config by set RootUrl, but in dotnet core version, only RoutePrefix can set, and i try it not work correct. – mofee Oct 15 '18 at 09:43

2 Answers2

3

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;
                });
            });
mofee
  • 135
  • 1
  • 9
1

You could try rewriting your urls using app.UseRewriter.

app.UseRewriter(new RewriteOptions()
    .AddRewrite(@"/cus/settings", "Chat$1", skipRemainingRules: true));

Note that this is untested and just written out of my head.

Philip
  • 11
  • 2