【问题标题】:Swagger No API definition providedSwagger 未提供 API 定义
【发布时间】:2021-12-07 18:53:55
【问题描述】:

我在我的 Blazor WebAssembly 项目中实现了 Swagger,但 swagger 似乎无法识别我的任何 API?似乎无法弄清楚这个问题,并想知道是否有人知道为什么会发生这种情况。提前致谢。

在服务器项目中安装了 Swashbuckle.AspNetCore

我的控制器不使用传统的Route(["api/controller"]),而是使用Route(["controller"]),我认为这不会导致此问题。

此外,所有控制器功能都已标记为 get/post/put,例如HttpGet[(...)] ...

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDatabaseDeveloperPageExceptionFilter();

        services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => {
                options.IdentityResources["openid"].UserClaims.Add("role");
                options.ApiResources.Single().UserClaims.Add("role");
            });

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("role");

        services.AddAuthentication()
            .AddIdentityServerJwt();

        services.AddControllersWithViews();

        services.AddRazorPages();

        services.Configure<IdentityOptions>(options =>
        options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);

       services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize);

        services.AddSwaggerGen();
    
    }

    // This method gets called by the runtime. Use th   is method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
            app.UseWebAssemblyDebugging();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseBlazorFrameworkFiles();
        app.UseStaticFiles();

        app.UseRouting();
        app.UseIdentityServer();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseSwagger();
        app.UseSwaggerUI();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
            endpoints.MapFallbackToFile("index.html");
        });
    }
}

【问题讨论】:

    标签: swagger blazor swagger-ui blazor-webassembly swashbuckle.aspnetcore


    【解决方案1】:

    文档因各种选项和版本而有些分散。但相关的行是:

    • 如果您使用的是基于端点的路由,请添加端点。
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapSwagger();     // add this line
        endpoints.MapFallbackToFile("index.html");
    });
    

    【讨论】:

    • 感谢您的回复。使用endpoints.MapSwagger()时,我似乎得到了iendpointroutebuilder does not contain definition for mapswagger
    • 嗯,您的 Blazor 和 Swashbuckle 版本到底是什么?它应该可以工作。
    • 我正在使用 5.0 版的 Swashbuckle,这是从 NuGet 安装时选择的选项。不知道如何检查 blazor 版本
    • 最新的稳定版是 6.2.3 。版本 5 似乎很古老。只需升级。
    • 您知道我可能需要升级到哪个版本吗?我认为在某一点之后,包变得与我的项目不兼容
    猜你喜欢
    • 1970-01-01
    • 2020-06-06
    • 2019-11-13
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多