问题
I am creating a Blazor WASM and I need to include logic within the index.html file to load different css/js files and apply various css styles to body tag based on some conditions. However since the root file is static (www/index.html) and the app tag is nested inside the body tag this is not possible.
So I followed this link and created an Index.cshtml file (based on the static index file) in the server project and changed the endpoint in the Startup of the server project to MapFallbackToPage instead like below.
Startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToPage("/Index");
});
Index.cshtml
@page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Server Blazor</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
</head>
<body>
<app>opening...</app>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
This works however when refreshing any page the routing breaks and shows below error. The same error is shown even if an invalid url is entered (instead of the 404 message).
An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches: /Index /Index Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
How can this be configured to work or is there a better way to add logic to the head/body tags of a Blazor WASM application?
来源:https://stackoverflow.com/questions/64412156/blazor-webassembly-with-index-file-configuration-logic-causing-ambiguousmatchexc