How to stop Hosted Blazor WASM from navigating to api

125 Views Asked by At

I have designed my blazor wasm application and its hosted in blazor server app, therefore both applications share the same base url. When i launch my server app, it successfully launchers the wasm app. My problem is that when i hit the refresh button on the web browser , the api/server app loads in the browser instead of refreshing the blazor web assembly. How can i stop this?

This is my Server Startup class public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }


    public void ConfigureServices(IServiceCollection services)
    {

        services.AddRazorPages();
        services.AddControllersWithViews();

        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen();

        services.ConfigureCors();
        services.ConfigureIISIntegration();
        services.ConfigureLoggerService();
        services.ConfigureMySqlContext(Configuration);
        services.ConfigureRepositoryWrapper();
        services.AddAutoMapper(typeof(Startup));
        services.AddControllers();
        services.AddRouting(options => options.LowercaseUrls = true);
        
       




    }


    // 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.UseSwagger();
            //app.UseSwaggerUI();
            app.UseWebAssemblyDebugging();

        }
        else
        {
            app.UseHsts();
        }

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

        //app.UseCors("CorsPolicy");


        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.All
        });


       app.UseRouting();

        app.UseAuthorization();
        app.UseMiddleware<ApiKeyMiddleware>();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
            endpoints.MapFallbackToFile("index.html");
            

        });
    }
}

My WASM Program.cs file

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("#app");

     
        builder.Services.AddScoped(sp => new HttpClient(new AddHeadersDelegatingHandler())
        {
            BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
        });

        builder.Services.AddAntDesign();
        builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));
        builder.Services.AddScoped<IChartService, ChartService>();
        builder.Services.AddScoped<IProjectService, ProjectService>();
        builder.Services.AddScoped<IUserService, UserService>();
        builder.Services.AddScoped<IAccountService, AccountService>();
        builder.Services.AddScoped<IProfileService, ProfileService>();
        
        await builder.Build().RunAsync();



    }
}
1

There are 1 best solutions below

0
On

I was looking for the same issue, when I found your question, of course no answers :)

BTW I finally managed to solve it, hopefully it will help you out, in my case the page controller and the Api controller had the same URL, I just had to change the Api controllers route, for example from this:

[Route("[controller]")]
public class CategoryController : ControllerBase

to this:

[Route("api/[controller]")]
public class CategoryController : ControllerBase

Cheers!