Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 347
N-ate Avatar asked Apr 07 '20 01:04

N-ate


2 Answers

Just set the Title property when calling AddSwaggerDocument

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerDocument(settings =>
    {
        settings.Title = "My Own Title";
    });
}
like image 84
Julian Avatar answered Sep 18 '22 19:09

Julian


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

like image 29
Brando Zhang Avatar answered Sep 17 '22 19:09

Brando Zhang