c# - 为公共用户设置单独的布局
问题描述
在 Blazor WebAssembly 中,如何为登录用户和未登录用户提供单独的布局?
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<Authorizing>
<text> Authotizing ...</text>
</Authorizing>
</AuthorizeRouteView>
/*Something like this:*/
<NotAuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(NotAuthorizedLayout)">
<Authorizing>
<text> Authotizing ...</text>
</Authorizing>
<NotAuthorized>
<text></text>
</NotAuthorized>
</NotAuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
或者换句话说,如何在 Blazor 中的不同布局之间切换?
解决方案
目前不存在第二个参数AuthorizeRouteView
来为“公共”用户设置特定布局。但是存在一个简单的解决方案,这就是您可以轻松处理这种情况的方法。
假设您有 2 个布局准备好申请 auth 和 pub 用户:
AuthL.razor
对于身份验证用户。PubL.razor
对于公共用户。
此时,您可以重写MainLayout.razor
使用AuthorizeView 组件来设置正确的布局:
@inherits LayoutComponentBase
<AuthorizeView>
<Authorized>
<AuthL Body=@Body />
</Authorized>
<NotAuthorized>
<PubL Body=@Body />
</NotAuthorized>
</AuthorizeView>
就这样。
推荐阅读
- jquery - 滚动 jquery var 上的粘性导航未更新
- html - 选择与文本内联的元素
- php - 在 php 中使用 sqlsrv 调用 SQL 存储过程并获取返回成功或失败
- ios - swift for循环遍历json对象不会为字符串添加值
- laravel - Laravel Horizon worker:空闲内存使用
- c++ - 如何将一些文本保存到包含空格的字符串中?
- android - 如何使用 FCM 在颤振中推送通知
- javascript - 我的自定义滑块图像显示在原始域上,但不在虚拟域上(仅适用于移动设备)
- python - Docker Python API 更新方法生成 TypeError: unhashable type: 'dict'
- python - 错误:无法为使用 PEP 517 且无法直接安装的 esmre 构建轮子