LoginSignup
1
0

.NET8 Blazor Identity WebApp では対話型的なコンポーネントをメイン レイアウトに追加

Last updated at Posted at 2024-03-22

.NET8 Blazor Identity WebApp では対話型的なコンポーネントをMainLayout.razorに追加

Radzen レイアウトコンポーネントを Blazor WebApp のメイン レイアウトに追加したかったが、ヘッダーには対話型コンポーネントであるユーザー メニューが含まれていたため、MainLayout.razorのレンダー モードを対話型にする必要がありました。

第1問題

  1. 最初の問題は、@rendermode InteractiveServer ディレクティブを単に Blazor レイアウトに追加することはできず、以下のように Components\App.razor ファイル内の <HeadOutlet /> タグと <Routes /> タグに属性として追加する必要があることです:
        <HeadOutlet rendermode="InteractiveServer" />
    </head>
    <body>
        <Routes rendermode="InteractiveServer" />
    

第2問題

  1. 次の問題は、アプリケーションを起動すると、ログイン ページのレンダリングが何度もループすることでした。
  2. 解決策は、認証ルートを除くすべてのルートにグローバル「InteractiveServer」レンダリング モードを条件付きで設定するコードを追加することでした:
        <HeadOutlet @rendermode="RenderModeForPage" />
    </head>  
    <body>
        <Routes @rendermode="RenderModeForPage" />
        <script src="_framework/blazor.web.js"></script>
        <script src="_content/Radzen.Blazor/Radzen.Blazor.js"></script>
    </body>
    
    </html>
    
    @code {
        [CascadingParameter]
        private HttpContext HttpContext { get; set; } = default!;
    
        private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account") ? null : InteractiveServer;
    }
    

メインレイアウト

  1. Components\Layout\MainLayout.razor ファイルに <RadzenComponents /> などを移します:
    @inherits LayoutComponentBase
    @using Microsoft.AspNetCore.Authorization
    @using Microsoft.AspNetCore.Components.Sections
    @using Microsoft.AspNetCore.Identity
    @using System.Diagnostics
    @using System.Security.Claims
    
    @inject AuthenticationStateProvider GetAuthenticationStateAsync
    @inject NavigationManager NavigationManager
    @inject IJSRuntime JsRuntime
    
    <RadzenComponents />
    
    <RadzenLayout>
        // ...
    

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0