Blazor-直接访问页面URL不起作用,点击通过Navmenu Works单击
我有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
Here is the _Host.cshtml
file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解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 throughNavMenu
or built-in C# component likeNavigationManager
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@page
s discovery then load the internal navigation mechanism.以防万一仍然开放:我设法通过添加后备页面来启动并运行它。这可以通过将以下内容添加到
startup.cs
: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
: