Blazor WebAssembly with index file configuration logic causing AmbiguousMatchException

点点圈 提交于 2021-01-29 18:36:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!