NSwag doesn't render UI on .Net core 2.1

355 Views Asked by At

Recently I have installed NSwag.AspNetCore to my ASP.Net Core 2.1 project to generate api document, This is my source code in Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Add swagger.
    services.AddSwagger();

    // Construct mvc options.
    services.AddMvc(mvcOptions =>
        {
            ////only allow authenticated users
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                .Build();

            mvcOptions.Filters.Add(new AuthorizeFilter(policy));
        })
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    ;
}

public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
{
        app.UseSwaggerUi3WithApiExplorer(settings =>
        {
            settings.GeneratorSettings.DefaultPropertyNameHandling =
                PropertyNameHandling.CamelCase;

            settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("API Key"));

            settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("API Key",
                new SwaggerSecurityScheme
                {
                    Type = SwaggerSecuritySchemeType.ApiKey,
                    Name = "Authorization",
                    Description = "Copy 'Bearer ' + valid JWT token into field",
                    In = SwaggerSecurityApiKeyLocation.Header
                }));
        });

        app.UseStaticFiles();

        // Enable MVC features.
        app.UseMvc();

}

At first, I ran the project and it worked. But at the second time, after I installed NSwag.Annotation and uninstall it. When I start my application, there is no Swagger UI at the endpoint: <myhost>/swagger and status is: 404

Can anyone help ? Thanks,

0

There are 0 best solutions below