Can I use Swashbuckle to generate Swagger UI from project with Blazor C# I know that swaschbuckle requires MVC and that you cannot have both of them in same project. But is there any way around it.
2 Answers
I have resolved the issue by using the Balzor template for W ASP.Net core 3.0 application. And following this guide:Getting started with swashbuckle ASP.Net Core 3.0
I have used pre-release version 5.0.0-rc3 of Swashbuckle since version 4.0.1 failed at startup.
My startaup for anyone facing the same issue:
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");
});
}
}
1 Comment
Swagger is basically to expose your APIs on UI and if there is API controller concept then yes you can definitely use the swagger for blazor APIs....
Please go through the below URL and I am pretty sure that it will help you get what you want..
If you still facing any issue then please let me know I will try to give you an practical example of this...
https://www.talkingdotnet.com/create-a-crud-app-using-blazor-and-asp-net-core/