For some kind of reasons, Blazor not show the "Not Found" page, created in App.razor
, when users type on their browser a endpoint like a files (example: localhost:5001/unexist.file)
Example when endpoint contains points
App.razor
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" >
....
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
...
My custom Not Found page content
...
</LayoutView>
</NotFound>
</Router>
<NavigationTracker />
</CascadingAuthenticationState>
This work correctly only if endpoint doesn't contains any point characters. There is a possibility to control this?
CodePudding user response:
Looking at Microsoft doc I found this helpful information
Basically the method depends on the hosting model:
In your case Blazor server(as your tag says). to do it we need change program.cs as follow:
app.MapFallbackToPage("/{param?}", "/_Host");
In blazor web assembly hosted in ASP.Net we change it as follows:
app.MapFallbackToFile("/{param?}", "index.html");
explanation from Microsoft docs:
the server-side default route template assumes that if the last segment of a request URL contains a dot (.) that a file is requested. For example, the URL https://localhost.com:5001/example/some.thing is interpreted by the router as a request for a file named some.thing. Without additional configuration, an app returns a 404
Last thing I discovered while searching, is that I was unable to change the route behavior when running web assembly standalone without dotnet host. If I were to publish it, it can be change depending on the host, like changing web.config, but I had no luck when in debugging mode.
CodePudding user response:
If you use a previous version of .Net, or you have an old scaffolding, you need change this configuration in Program.cs
like this:
...
app.UseEndpoints(endpoints =>
{
...
endpoints.MapFallbackToPage("/{param?}", "/_Host");
});