ASP.Net Core Razor with Blazor WASM and Wildcard URL

405 Views Asked by At

I'm creating a website with a Blazor WASM as the admin, and a Core Razor is the main site. I didnt have any problem so far mixing both, but on the main site I have a Wildcar url, that gets anything "/{*url}" if I enable it the Blazor stop working, it is possible to do it? Here is my Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages(options =>
{
    options.Conventions.AddPageRoute("/Dynamic/index", "{*url}");
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
else
{
    app.UseWebAssemblyDebugging();
}
app.UseStaticFiles();

app.UseBlazorFrameworkFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.MapFallbackToFile("admin/{*path:nonfile}", "admin/index.html");

app.Run();

thanks

1

There are 1 best solutions below

0
Eduardo On

I was able to figure it out. this is my code in case someone has the same problem:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages(options =>
{
    options.Conventions.AddPageRoute("/Dynamic/index", "{*url}");
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
else
{
    //add for blazor client
    app.UseWebAssemblyDebugging();
}
app.UseStaticFiles();

//add blazor
app.UseBlazorFrameworkFiles();

app.MapWhen(context => context.Request.Path.StartsWithSegments("/admin"), appBuilder =>
{
    appBuilder.Use((context, nxt) =>
    {
        context.Request.Path = "/admin" + context.Request.Path;
        return nxt();
    });

    appBuilder.UseStaticFiles();
    appBuilder.UseRouting();

    appBuilder.UseAuthentication();
    appBuilder.UseAuthorization();

    appBuilder.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("admin/{*path:nonfile}", "admin/index.html");
    });
});

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();