1

最近我已经安装NSwag.AspNetCore到我的ASP.Net Core 2.1项目以生成 api 文档,这是我在Startup.cs文件中的源代码:

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();

}

起初,我运行了这个项目,它成功了。但在第二次,在我安装NSwag.Annotation和卸载它之后。当我启动我的应用程序时,端点没有 Swagger UI:<myhost>/swagger并且状态为:404

任何人都可以帮忙吗?谢谢,

4

0 回答 0