代码之家  ›  专栏  ›  技术社区  ›  Ibanez1408

JSON和UI端点的核心托管Blazor WASM Swagger返回404

  •  0
  • Ibanez1408  · 技术社区  · 7 月前

    这是我的全部代码

    namespace AlprDashboard.Server.Domain
    {
        public static class ServiceCollectionExtension
        {
            
           // OTHER CODES...
    
            public static void UseSwaggerExtension(this IServiceCollection services)
            {
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "ALPR Dahsboard API", Version = "v1" });
                });
            }
        }
    }
    
    'Program.cs'
    
    using AlprDashboard.Server.Domain;
    using Microsoft.AspNetCore.Hosting.StaticWebAssets;
    using Serilog;
    
    var builder = WebApplication.CreateBuilder(args);
    
    StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
    
    builder.Services.AddControllersWithViews();
    builder.Services.AddRazorPages();
    
    builder.Host
            .UseSerilog((hostingContext, loggerConfiguration) =>
            {
                loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
            });
    
    builder.Services.ConfigureApplicationCors(builder.Configuration);
    builder.Services.ConfigureApplicationAuthentication(builder.Configuration);
    builder.Services.UseSwaggerExtension();
    builder.Services.ConfiguredExtensions();
    
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseWebAssemblyDebugging();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "ALPR Dahsboard API");
    });
    
    app.UseCors("ApplicationCors");
    
    app.UseHttpsRedirection();
    
    app.UseBlazorFrameworkFiles();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthentication();
    app.UseAuthorization();
    
    app.MapRazorPages();
    app.MapControllers();
    app.MapFallbackToFile("index.html");
    
    app.Run();
    

    当我运行它并转到https://localhost:5001/swagger/v1/swagger.json'或https://localhost:5001/swagger/index.html’,我得了404分。

    由于无法访问Swagger UI,我在这里缺少了什么?

    1 回复  |  直到 7 月前
        1
  •  1
  •   Tiny Wang    7 月前

    以下是我在blazor wsam asp.net核心托管项目中所做的工作。

    安装nuget包 Swashbuckle.AspNetCore

    enter image description here

    在托管服务器项目Program.cs中添加配置

    builder.Services.AddControllersWithViews();
    builder.Services.AddRazorPages();
    builder.Services.AddSwaggerGen();//add this line
    
    var app = builder.Build();
    
    if (app.Environment.IsDevelopment())
    {
        app.UseWebAssemblyDebugging();
        //add the next 2 lines
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    

    enter image description here

    推荐文章