如何更改 nswag 生成的默认信息标题?

How can I change the default info title produced by nswag?

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

我无法确定如何将 "My Title"(这是信息标题)更改为其他内容。

这是 swagger 页面:

这是我的注册码:

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

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

据我所知,如果要修改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?}");             
            });
        }
    }
}

结果:

调用时设置Title属性AddSwaggerDocument

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

对于 NSwag

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

对于在搜索如何在 .NET 6 中执行此操作时来到此处的任何人...

如果您在创建 .NET 6 Web API 项目时选中“启用 OpenAPI 支持”,您将获得一个包含一些基本 Swagger 配置的模板。我认为任何现有答案都不适用于该模板。如果在 .NET 6 项目中有更简单的方法来执行此操作,我将不胜感激。

创建一个 class 实现 IDocumentFilter:

public class TitleFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument doc, DocumentFilterContext context)
    {
        doc.Info.Title = "My Title";
    }
}

将您的文档过滤器添加到 SwaggerGenOptions:

builder.Services.AddSwaggerGen(o => o.DocumentFilter<TitleFilter>());