【问题标题】:Can I use Swashbuckle to generate Swagger UI from Blazor project c#我可以使用 Swashbuckle 从 Blazor 项目 c# 生成 Swagger UI
【发布时间】:2019-09-24 16:29:23
【问题描述】:

我可以使用 Swashbuckle 从 Blazor C# 项目中生成 Swagger UI 我知道 swaschbuckle 需要 MVC,并且您不能将它们都放在同一个项目中。 但是有什么办法可以解决。

【问题讨论】:

    标签: c# asp.net-core swagger swagger-ui blazor


    【解决方案1】:

    我已经通过使用用于 W ASP.Net core 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");
            });
        }
    }
    

    【讨论】:

    • 谢谢你这帮助我现在只是配置jwt和auth
    【解决方案2】:

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

    请通过下面的网址,我很确定它会帮助你得到你想要的..

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

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

    【讨论】:

    • 是否仍然可以使用 ASP.net Core 3.0 Blazor(带有 Razor 页面路由)向项目添加控制器?
    • 是的,他们可能在给定的链接中提到了如何实现这一点......
    猜你喜欢
    • 2021-04-13
    • 2020-10-30
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多