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



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

我已经通过使用 W ASP.Net core 3.0 应用程序的 Balzor 模板解决了这个问题。并遵循本指南:开始使用虚张声势 ASP.Net 酷睿 3.0

自从 4.0.1 版在启动时失败以来,我一直使用预发布版本的 Swashbuckle 5.0.0-rc3。

我的启动为面临相同问题的任何人提供:

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");
});
}
}

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

请浏览以下URL,我很确定它将帮助您获得所需的内容。

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

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

最新更新