如何在Blazor Server中有条件地设置组件的MainLayout。

huangapple go评论57阅读模式
英文:

How to set MainLayout conditionally for a component in Blazor Server

问题

在我的Blazor Server项目中,我有一个MainLayout.razor组件,它是整个应用程序的主要布局。在这个MainLayout的OnInitialized事件中,我正在检查身份验证状态,只有已经通过身份验证的用户才能访问进一步的组件。

有一个FAQ组件,它应该可以由已经通过身份验证和未通过身份验证的用户访问。对于这两种用户,某些功能会有所不同。

我创建了另一个不需要身份验证的布局组件。

现在我卡在如何根据已通过身份验证和未通过身份验证的用户动态设置FAQ组件的布局。

App.razor

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <NotFound>
            <PageTitle>Not found</PageTitle>
            <LayoutView Layout="@typeof(MainLayout)">
                <p role="alert">Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

MainLayout.razor

[CascadingParameter]
public Task<AuthenticationState> AuthenticationState { get; set; }

protected async override Task OnInitializedAsync()
{
    await base.OnInitializedAsync();

    var authState = await AuthenticationState;
    if (authState.User.Identity.IsAuthenticated)
    {
        // 登录后的某些功能
    }
    else
    {
        // 导航到登录界面
    }
}

TempLayout.razor(此组件用于未经身份验证的用户)

@inherits LayoutComponentBase

<!-- 开始:内容 -->
<div class="app-content container center-layout mt-2">
    <div class="content-overlay"></div>
    <div class="content-wrapper">
        <div class="content-header row">
        </div>
        @Body
    </div>
</div>
<!-- 结束:内容 -->

这些是您提供的代码的翻译部分。

英文:

In my Blazor Server project, I have a MainLayout.razor component which is a master layout for the whole application. On this MainLayout's OnInitialized event I'm checking the authentication state and only authenticated users can access the further components.

There is a FAQ component which should be accessible by authenticated and non-authenticated users as well. For both of such users, some functionality would be different.

I have created another Layout Component which doesn't need authentication.

Now I'm stuck at the point that how I can dynamically set the layout of the FAQ component according to authenticated and non-authenticated users.

App.razor

&lt;CascadingAuthenticationState&gt;
&lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
    &lt;Found Context=&quot;routeData&quot;&gt;
        &lt;AuthorizeRouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot; /&gt;
        &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
    &lt;/Found&gt;
    &lt;NotFound&gt;
        &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
        &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
            &lt;p role=&quot;alert&quot;&gt;Sorry, there&#39;s nothing at this address.&lt;/p&gt;
        &lt;/LayoutView&gt;
    &lt;/NotFound&gt;
&lt;/Router&gt;

</CascadingAuthenticationState>

MainLayout.razor

 [CascadingParameter]
public Task&lt;AuthenticationState&gt; AuthenticationState { get; set; }

protected async override Task OnInitializedAsync()
{
    await base.OnInitializedAsync();

    var authState = await AuthenticationState;
    if (authState.User.Identity.IsAuthenticated)
    {
        //Some functionality after login
    }
    else
    {
       //Navigate to the Login screen
    }

}

TempLayout.razor (this component to be used for non-authenticated users)

@inherits LayoutComponentBase

 &lt;!--Start: Content--&gt;
&lt;div class=&quot;app-content container center-layout mt-2&quot;&gt;
&lt;div class=&quot;content-overlay&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;content-wrapper&quot;&gt;
    &lt;div class=&quot;content-header row&quot;&gt;
    &lt;/div&gt;
    @Body
&lt;/div&gt;

</div>
<!--End: Content-->

答案1

得分: 1

制作两个组件:一个用于已验证用户,另一个用于未验证用户。为每个组件设置不同的布局。

英文:

Make two components; one for authenticated and other for un-authenticated users. Set different layout for each

答案2

得分: 0

你可以将 app.razor 包装在 authorizeView 中。这样,你可以根据当前的授权状态聚合到正确的布局中。

或者

你只使用一个 layout.razor 并创建一些条件性的 cshtml 来显示正确的布局。

正如 @ruikai_feng 提到的,这个链接可能对你有帮助:https://github.com/dotnet/aspnetcore/issues/39456。

英文:

You could wrap the app.razor in a authorizeView. This way you can aggregate to the correct layout depending on which autorization state you currently have.

OR

you use only one layout.razor and you make some conditional cshtml to show the correct layout.

As @ruikai_feng mentioned this might help you https://github.com/dotnet/aspnetcore/issues/39456

huangapple
  • 本文由 发表于 2023年7月10日 14:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定