我正在使用 .NET 5.0 创建 Blazor 应用程序,启动应用程序时需要传递一个在整个应用程序中使用的参数,而不仅仅是在应用程序的一页上。
也就是说,在对应用程序进行编程时,它运行在
localhost:port/
在端口之后,我想定义参数并将其在主布局中读取并发送到正文(我的主布局已附加)。
在代码正文中,只有2个页面,并且两个页面都消耗组件,我在网上找到了如何将其作为级联参数或简单甚至索引参数传递,但没有一个对我有用.
所以我需要的是这样的:
localhost:port/param
我不知道是否可以这样做,因为该应用程序必须是通过单击从另一个桌面应用程序打开的链接,并且浏览器会打开并包含该参数
或者我不知道如何告诉我的应用程序初始页面不是
/
而是 /page/param
。
但我想我只在该页面上收到它,而且我还有一个下拉菜单,我可以在两个页面之间导航,这是一个组件,但至少我已经看到了如何向它传递参数
MainLayout
:
@inherits LayoutComponentBase
<div class="page d-flex" >
<section class="navbar navbar-expand-lg navbar-light pr-lg-2 flex-lg-row w-100 position-fixed" style="z-index: 2; background-color: @vc_rojoFrisby;">
<div class="container-fluid p-1 d-flex justify-content-between align-items-center">
<nav style="color: white; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 20px;">Restaurante Rastreo Domi @vc_RestId</nav>
<div class="text-center">
<img src="/img/icono_frisby.png" alt="logo_frisby" class="img-fluid" style="max-height: 40px; height:auto; width: auto;">
</div>
<div class="d-flex align-items-center">
<nav style="color: white; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 20px; margin-right: 10px;"> Restaurante: F54</nav>
<DropdownMenu/>
</div>
</div>
</section>
<CascadingValue Value="@vc_RestId">
<div class="mt-2 ml-5 mt-lg-4 mt-xl-5 mt-md-5 mt-sm-5 position-relative" style="flex-grow: 1; width:100%;">
@Body
</div>
</CascadingValue>>
</div>
@code
{
public static string vc_rojoFrisby = "#E00109";
[Parameter]
public string vc_RestId { get; set; } = "";
}
我尝试在网上搜索,我要求它来聊天 gpt 和 Gemini
我不知道你的项目结构,但要通过 URL 传递参数,你必须使用类似的东西
@page “/{param}“
在根级别或您的 MainLayout 上。有关传入查询参数的信息,请参阅MSDC。然后,您可以将此参数定义为级联参数,并且您的组件应该能够访问它。