我可以使用 Swashbuckle 从 Blazor 项目 c# 生成 Swagger UI

Can I use Swashbuckle to generate Swagger UI from Blazor project c#

我可以使用 Swashbuckle 从带有 Blazor C# 的项目中生成 Swagger UI 我知道 swaschbuckle 需要 MVC,并且你不能在同一个项目中同时拥有它们。 但是有没有办法解决呢。

Swagger 基本上是在 UI 上公开您的 APIs,如果有 API 控制器概念,那么是的,您绝对可以将 swagger 用于 blazor APIs ....

请仔细阅读下面的内容URL,我很确定它会帮助您得到想要的东西。

如果您仍然遇到任何问题,请告诉我,我会尝试给您一个实际的例子...

https://www.talkingdotnet.com/create-a-crud-app-using-blazor-and-asp-net-core/

我已经通过为 W ASP.Net 核心 3.0 应用程序使用 Balzor 模板解决了这个问题。并遵循本指南:Getting started with swashbuckle ASP.Net Core 3.0

我使用 Swashbuckle 的预发布版本 5.0.0-rc3,因为版本 4.0.1 在启动时失败。

我为面临同样问题的任何人创办的公司:

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

    public IConfiguration Configuration { get; }

    // 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.AddControllers();
        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
        services.AddSingleton<WeatherForecastService>();
    }

    // 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.UseDatabaseErrorPage();
        }
        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();
        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

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

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });
    }
}