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

4

19

I'm using NSwag for .NET Core 3.1. Everything works correctly.

I can't determine how to change "My Title" (which is the info title) to something else.

Here is the swagger page:

swagger info title

Here is my registration code:

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

Any help is very much appreciated. Thanks!

Jolly answered 7/4, 2020 at 1:30 Comment(0)
P
18

Just set the Title property when calling AddSwaggerDocument

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerDocument(settings =>
    {
        settings.Title = "My Own Title";
    });
}
Pliam answered 14/4, 2020 at 11:49 Comment(1)
In case anyone is here looking for an answer about how to change the window title as well, see this answer to another question https://mcmap.net/q/673098/-is-it-possible-to-change-html-title-with-swashbuckle-and-swagger-uiLutz
F
10

For NSwag

services.AddOpenApiDocument(document => {
   document.Title = "Your Title";
});
Fresnel answered 15/6, 2021 at 17:10 Comment(0)
P
7

For anyone who landed here while searching for how to do this in .NET 6...

If you check "Enable OpenAPI support" when creating a .NET 6 Web API project, you get a template with some basic Swagger config. I don't think any of the existing answers are applicable to that template. If there's a simpler way to do this in a .NET 6 project, I'd appreciate an edit.

Create a class that implements IDocumentFilter:

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

Add your document filter to the SwaggerGenOptions:

builder.Services.AddSwaggerGen(o => o.DocumentFilter<TitleFilter>());
Paulownia answered 12/5, 2022 at 23:19 Comment(1)
I used something like this: builder.Services.AddSwaggerGen(o => o.SwaggerDoc("v1", new OpenApiInfo { Title = "My Title" }));Piscina
E
3

As far as I know, if you want to modify the nswagtitle. You should modify AddSwaggerDocument config setting in the ConfigureServices method as below:

Details you could refer to below codes:

        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"
                //};
            };

        });

Details startup.cs codes:

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?}");             
            });
        }
    }
}

Result:

enter image description here

Elvia answered 7/4, 2020 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.