Blazor-直接访问页面URL不起作用,点击通过Navmenu Works单击

发布于 2025-02-11 07:41:48 字数 552 浏览 1 评论 0原文

我有2个组件:

  • 大火
  • 测试

大餐组件的 ,我可以通过Navmenu或直接通过浏览器访问而无需任何问题。 URL:https:// localhost:5000/blazor

用于测试组件,我可以通过navmenu访问,但是如果直接通过浏览器访问,它将显示空白页 (开发人员工具显示 404错误) URL:https:// localhost:5000/test

唯一的区别是_HOST.CSHTML 文件。

这是test.razor file

​/xn1ss.png“ rel =” nofollow noreferrer“>

I have 2 components:

  • Blazor
  • Test

For the Blazor component, I can access via the NavMenu or directly via browser without issues.
URL: https://localhost:5000/blazor

For the Test component, I can access via the NavMenu, but it will show a blank page if directly accessing via the browser (Developer Tools show a 404 error)
URL: https://localhost:5000/test

The only difference is that "blazor" component is specified as the @page in the _Host.cshtml file.

Here is the Test.Razor file

enter image description here

Here is the _Host.cshtml file

enter image description here

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

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

发布评论

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

评论(2

慢慢从新开始 2025-02-18 07:41:48

如果我正确理解Blazor Server,则您的_HOST.CSHTML需要保持在“/”页面路径上,因为它是扎根于加载剃须刀组件的实际页面,也是URL导航的参考,但是您也可以给它多个@page属性,因此它将从多个URL响应/显示。

至于为什么直接通过URL访问404,而在使用navmenu时返回404,则首先点击“/blazor”时,所有的剃须刀组件(带有或没有的Razor Files(Razor Files) @page属性)被发现,您的应用程序'session'开始,并且每当您浏览navmenu或内置的C#组件时,例如navigationmanager'sessig 。但是,每当您直接击中URL时,您的浏览器都会直接询问该特定路径,然后跳过实际上执行@page S Discovery的“/blazor”路径中的页面,然后加载内部导航机制。

If i understand Blazor Server correctly, your _Host.cshtml needs to stay on "/" page path, as it is the actual page rooted to load your razor components, as also reference for URL navigation, but you can also gives it multiple @page attribute, so it would respond/shown from multiple URL.

As for why it is returns 404 when accessed directly via URL but not when with NavMenu, when you hit "/blazor" first, all of your razor components (.razor files with or with without @page attribute) get discovered, your app 'session' start and whenever you navigate through NavMenu or built-in C# component like NavigationManager the 'session' persist. But whenever you hit the URL directly, your browser ask directly for that specific path, skipping the page in "/blazor" path that actually do the @pages discovery then load the internal navigation mechanism.

海之角 2025-02-18 07:41:48

以防万一仍然开放:我设法通过添加后备页面来启动并运行它。这可以通过将以下内容添加到startup.cs

app.MapFallbackToPage("/_Host");

Just in case that this is still open: I have managed to get it up and running by adding a fallback page. This can be done by adding the following to the Startup.cs:

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