Search code examples
asp.net-coreblazor

I have a Blazor ASP.NET Core 8.0 Routing Error


I'm at a loss here when it comes what appears to be a routing error in my Blazor ASP.NET Core 8.0 application. It's loading the main screen which is at page /. There is another file called _Host.cshtml that also has page /, but when I remove that, I get an error:

stdout: fail:
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.
Matches:

Blazor static files

at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(Span1 candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, Span1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.SelectAsync(HttpContext httpContext, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatch|10_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

I have looked at all my routes and I cannot find two that are the same. I'll keep looking since I have a LOT of razor files to check, but could there be another angle I could be looking at to solve this problem or is it really just two of my razor files have the same route?


Solution

  • In .Net 6 blazor server we have _Host.cshtml which doesn't exist in .Net 8 blazor web app(.net 8 doesn't have blazor server, but have blazor web app instead). We can see from the migration guidance that we need to remove _Host.cshtml.

    Move the content in the _Host page (Pages/_Host.cshtml) to the empty App.razor file. Proceed to make the following changes to the App component.

    In blazor server, we have app.MapFallbackToPage("/_Host"); in Program.cs so that when a request to visit the root path, it comes to _Host which configures the initial setup and routing for the server-side aspect of the Blazor application. While the @page "/" in Index.raozr defines the content to be displayed for the root URL (/) within the client-side Blazor application, which play the role somewhat like the client-side route path. Just like in normal client-side app uses html file name to work as the route.

    In my blazor server .net 6 application when I comment @page "/" in _Host.cshtml, I will get error like below which is different than yours.

    enter image description here

    I'm afraid that before we trying to find the Routing definition for causing AmbiguousMatchException, we could firstly check the migration guidance and check whether we missed any steps or not. In my test, if I just not deleting the _Host.cshtml, it won't cause your exception too.