The "standard" Blazor WASM application is hosted inside a static HTML page; e.g., index.html
. Due to certain requirements, I want to host Blazor inside a Razor pages application.
What I did is starting from a "standard" Blazor WASM application, removed the static files, because I do not need them, moved the content of index.html
to the Wasm.cshtml
, and change endpoints.MapFallbackToFile("index.html");
to endpoints.MapFallbackToPage("/Wasm");
.
Everything seemed to be working as expected; I can run the application and navigate to the different pages I have in Blazor.
However, things fall apart when I try to access a page using its URL; e.g., http://mysite/counter
, where /counter
is a page in Blazor, and I get the following error:
An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:
/Wasm
/Wasm
Can someone help me identify what I am doing wrong?
P.S.:
OK, based on what you've written so far take a look at ShaunCurtis/Blazor-Experimental on Github. It's a temporary Repo for some experimental code. Ignore BlazorTest. The startup project is Blazor-Experimental.
The default page is a normal razor page. It's a mixed Razor, Blazor Server and Blazor WASM site. All the WASM routes look like wasm/fetchdata
, so we have different URLs for all the Server and WASM "Pages".
Startup differentiates URLs using multiple endpoints, so any URL that is in the "scope" of the Blazor WASM application gets set to _wasm.cshtml
. Anything else that can't be mapped directly is in the "scope" of the Blazor Server Application at _host.cshtml
. All plan Razor pages on the site get served as is. You don't need the Blazor Server bit at all, just fallback to the default Razor page.
endpoints.MapFallbackToPage("/wasm/{**segment}", "/_wasm");
endpoints.MapFallbackToPage("/_Host");
To summarise the answer:
Wasm.cshtml
, and make sure the page route is not set; i.e., only @page
at the top of the page, so that it takes the default route /wasm
.index.html
in the Blazor WASM project into Wasm.cshtml
.<base href="/" />
on the page or the layout <head>
section.index.html
.*.razor
pages from the Blazor WASM project.Wasm.razor
to the Blazor WASM project and set its route to /wasm
; i.e., @page "/wasm"
.Startup.cs
in the Razor Pages project, add app.UseBlazorFrameworkFiles();
after app.UseStaticFiles();
.Startup.cs
, add endpoints.MapFallbackToPage("/wasm/{**segment}", "/wasm");
inside app.UseEndpoints()
lambda./wasm
. You should see the content of your Wasm.razor
in addition to whatever layout you have set. You will get the same result when you paste the URL http://whateveryoursiteis/wasm
.