没有显示文件端点的页面未找到页面

发布于 2025-02-06 05:12:46 字数 793 浏览 1 评论 0原文

由于某种原因,Blazor不显示app.razor在用户在浏览器上键入端点时创建的“找不到”页面,例如文件(示例:localhost:5001/unexist.file )

示例端点包含点 “在此处输入图像描述”

app.razor

<CascadingAuthenticationState>
  <Router AppAssembly="@typeof(Program).Assembly" >
    ....
    <NotFound>
       <LayoutView Layout="@typeof(MainLayout)">
       ...
       My custom Not Found page content
       ...
       </LayoutView>
    </NotFound>
  </Router>
  <NavigationTracker />
</CascadingAuthenticationState>

仅当端点不包含任何点字符时,此工作才正确。 有可能控制这一点吗?

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
enter image description here

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2025-02-13 05:12:46

Looking at Microsoft doc I found this

基本上该方法取决于托管模型:

在您的情况下, Blazor Server (如您的标签所说)。要做到这一点,我们需要更改

app.MapFallbackToPage("/{param?}", "/_Host");

app.MapFallbackToFile("/{param?}", "index.html");

程序

服务器端默认路由模板假设如果最后一个
请求URL的段包含请求文件的点(。)。
例如,url https://localhost.com:5001/example/sample/some.thit 是
路由器解释为对某些文件的请求。
没有其他配置,应用程序返回404


我在搜索时发现的404个最后一件事,这是我在没有dotnet host的情况下运行Web Assembly独立运行时无法更改路由行为。如果要发布它,它可以根据主机的不同而更改,例如更改web.config,但是在调试模式下我没有运气。

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.

月亮坠入山谷 2025-02-13 05:12:46

如果您使用以前的.NET版本,或者使用旧的脚手架,则需要在program.cs.cs中更改此配置:

...
app.UseEndpoints(endpoints =>
{
   ...
   endpoints.MapFallbackToPage("/{param?}", "/_Host");
});

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");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文