【问题标题】:How can I change the default info title produced by nswag?如何更改 nswag 生成的默认信息标题?
【发布时间】:2020-07-19 03:18:21
【问题描述】:

我正在为 .NET Core 3.1 使用 NSwag。一切正常。

我无法确定如何将“我的标题”(即信息标题)更改为其他内容。

这是招摇页面:

这是我的注册码:

app.UseOpenApi();
app.UseSwaggerUi3(c => c.DocumentTitle = "My Api");

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: c# asp.net-core-3.0 nswag


    【解决方案1】:

    调用AddSwaggerDocument时只需设置Title属性

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSwaggerDocument(settings =>
        {
            settings.Title = "My Own Title";
        });
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,如果你想修改 nswagtitle.您应该修改 ConfigureServices 方法中的 AddSwaggerDocument 配置设置,如下所示:

      详情可参考以下代码:

              services.AddSwaggerDocument(config =>
              {
                  config.PostProcess = document =>
                  {
                      //document.Info.Version = "v1";
                      document.Info.Title = "ToDo API";
                      //document.Info.Description = "A simple ASP.NET Core web API";
                      //document.Info.TermsOfService = "None";
                      //document.Info.Contact = new NSwag.OpenApiContact
                      //{
                      //    Name = "Shayne Boyer",
                      //    Email = string.Empty,
                      //    Url = "https://twitter.com/spboyer"
                      //};
                      //document.Info.License = new NSwag.OpenApiLicense
                      //{
                      //    Name = "Use under LICX",
                      //    Url = "https://example.com/license"
                      //};
                  };
      
              });
      

      详细startup.cs代码:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Threading.Tasks;
      using Microsoft.AspNetCore.Authentication.Negotiate;
      using Microsoft.AspNetCore.Builder;
      using Microsoft.AspNetCore.Hosting;
      using Microsoft.AspNetCore.Http;
      using Microsoft.Extensions.DependencyInjection;
      using Microsoft.Extensions.Hosting;
      
      namespace CoreNormalIssue
      {
          public class Startup
          {
              // This method gets called by the runtime. Use this method to add services to the container.
              // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
              public void ConfigureServices(IServiceCollection services)
              {
      
                  services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
       .AddNegotiate();
                  services.AddControllersWithViews();
      
                  services.AddSwaggerDocument(config =>
                  {
                      config.PostProcess = document =>
                      {
                          //document.Info.Version = "v1";
                          document.Info.Title = "ToDo API";
                          //document.Info.Description = "A simple ASP.NET Core web API";
                          //document.Info.TermsOfService = "None";
                          //document.Info.Contact = new NSwag.OpenApiContact
                          //{
                          //    Name = "Shayne Boyer",
                          //    Email = string.Empty,
                          //    Url = "https://twitter.com/spboyer"
                          //};
                          //document.Info.License = new NSwag.OpenApiLicense
                          //{
                          //    Name = "Use under LICX",
                          //    Url = "https://example.com/license"
                          //};
                      };
                  });
      
      
              }
      
              // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
              public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
              {
                  if (env.IsDevelopment())
                  {
                      app.UseDeveloperExceptionPage();
                  }
                  app.UseStaticFiles();
                  app.UseRouting();
                  app.UseAuthorization();
                  app.UseOpenApi();
                  app.UseSwaggerUi3();
      
      
      
      
                  app.UseEndpoints(endpoints =>
                  {               
                      endpoints.MapControllerRoute(
                              name: "default",
                              pattern: "{controller=Default}/{action=Index}/{id?}");             
                  });
              }
          }
      }
      

      结果:

      【讨论】:

        【解决方案3】:

        对于 NSwag

        services.AddOpenApiDocument(document => {
           document.Title = "Your Title";
        });
        

        【讨论】:

          猜你喜欢
          • 2021-11-14
          • 2017-04-24
          • 2012-01-06
          • 2012-05-16
          • 1970-01-01
          • 1970-01-01
          • 2022-07-11
          • 2016-08-22
          • 2022-09-22
          相关资源
          最近更新 更多