2

I'm using the '.net Core hosted' option on a Blazor WebAssembly solution.

This creates a .net Core Web API project for you as part of the solution.

If I created this myself and launch the project it will launch a Swagger UI interface to the API. If I launch the Server project in the '.net Core hosted' Blazor app, it launches the client app (as expected).

How can I get a Swagger UI for the Web API server app? (to test the API with).

I can find nothing on Google about this - this link didn't help: Can I use Swashbuckle to generate Swagger UI from Blazor project c#

thx.

3
  • For the same reason you'd ever use Swagger - seeing and testing endpoints. What's the alternative? Commented Jan 17, 2022 at 6:32
  • 1
    When developing in teams sometimes the Swagger UI is useful for testing etc. However in production if your providing swagger I would expect this to be for future client apps. This tells me maybe you should not bundle the first client with the API as hosted for cleaner deployments. All this aside it can be done. However I have not solved the issue of blazor not seeing the routes. brianparker.azurewebsites.net/swagger/index.html for instance will say Sorry, there's nothing at this address. but if you press CTRL-F5 it shows the swagger UI. Commented Jan 17, 2022 at 8:51
  • Thanks - went to /swagger/index.html - doesn't work even if I do ctrl F5 or press refresh Commented Feb 16, 2022 at 9:45

2 Answers 2

4

When you launch your Blazor app, manually go to /swagger/index.html and you will see the standard Swagger UI.

If you want the SwaggerUI to show up when you run your app, instead of the Blazor Client, edit your launchSettings.json and set the "launchUrl" value of your startup profile to /swagger/index.html

If you would like to add a link to it in your MainLayout.razor, and make sure the link only displays in the Development Environment.

EDIT: You need to make sure Swagger is set up on the server in your program.cs file. (Assuming Asp.NET Core 6, if using 5, there should be similar initialization code in your Startup.cs file)

var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyBlazor", Version = "v1" });
});
...
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
    app.UseSwagger();
    app.UseSwaggerUI(c => 
         c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyBlazor v1"));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Not working here either for localhost:1234/swagger/index.html or localhost:1234/swagger - this is a .net Core Hosted blazor app right...
Editted to add the configuration used for ASP.NET Core 6
Also, I believe the needed URL in older versions is /q/swagger-ui according to what I found at quarkus.io/guides/….
Note you also have to manually add the NuGet packages to the Server project
The Nuget Packages I had to add were: Swashbuckle.AspNetCore.Swagger, Swashbuckle.AspNetCore.SwaggerGen and Swashbuckle.AspNetCore.SwaggerUI
0

I got it working with .NET 8 using below structure:

Visual Studio project structure

Here's my Program.cs. Note that we don't need to use Startup class anymore. Both Blazor and Swagger can be initialized and set up in Program.cs.

using BlazorApp.Components;
using BlazorApp.Data;
using BlazorApp.Models;
namespace BlazorApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddServerSideBlazor();
            builder.Services.AddHttpContextAccessor();
            builder.Services.AddResponseCaching();
            builder.Services.AddResponseCompression();

            // Add Raozr services to the container.
            builder.Services.AddRazorComponents()
                .AddInteractiveServerComponents()
                .AddInteractiveWebAssemblyComponents();

            // Database Contexts
            builder.Services.AddDbContextFactory<LendingContext>();
            builder.Services.AddDbContextFactory<ReportCenterContext>();

            // Services
            builder.Services.AddSingleton<PortfolioService>();
            builder.Services.AddSingleton<ReportService>();
            builder.Services.AddSingleton<CollateralService>();

            builder.Services.AddQuickGridEntityFrameworkAdapter();

            // Swagger
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title = "API",
                    Version = "v1",
                });
            });

            builder.Services.AddRazorPages();
            // Add Controllers
            builder.Services.AddControllers();
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseWebAssemblyDebugging();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error", createScopeForErrors: true);                
                app.UseHsts();
            }

            app.UseSwagger();
            app.UseSwaggerUI();

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            //app.UseAntiforgery();

            app.MapRazorComponents<App>()
                .AddInteractiveServerRenderMode()
                .AddInteractiveWebAssemblyRenderMode()
                .AddAdditionalAssemblies(typeof(Client._Imports).Assembly);

            // Configure the HTTP request pipeline.
            app.UseRouting();
            app.UseAntiforgery();
            app.MapControllers();

            app.MapRazorPages();
            app.MapDefaultControllerRoute();
            app.Run();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.