Can I use global interactivity along with per page interactivity in same project of .net 8 blazor web app ?

Kuldeep Y 41 Reputation points
2024-05-28T14:30:31.64+00:00

I have created a blazor web app application and I want to use global auto render mode along with per page interactivity mode in same project, is it possible ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,562 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,332 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,470 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,966 Reputation points
    2024-05-28T15:27:46.63+00:00

    yes. the default is static and the components can override the default. a running Blazor app only support one interactive mode (client or server). switching modes (like client to server, or prerender to interactive), unloads a Blazor instance and starts a new instance.


  2. Jerry Fu - MSFT 571 Reputation points Microsoft Vendor
    2024-05-29T01:50:26.8566667+00:00

    Hi, @Kuldeep Y

    Sure, you could use global dynamic rendermode in App.razor for this purpose. Try modify App.razor like following :

    ...
        <HeadOutlet @rendermode="RenderModeForPage()" />
    ...
        <Routes @rendermode="RenderModeForPage()" />
    ...
    @code {
            [CascadingParameter]
            private HttpContext HttpContext { get; set; } = default!;
            //if the page route is "/Counter" ,the rendermode is set to null, equals to SSR
            private IComponentRenderMode? RenderModeForPage(){
                if (HttpContext.Request.Path.StartsWithSegments("/Counter"))
                {
                    return null;
                }
                else
                {
                    return InteractiveAuto;
                }
            }
    }
    

    Then the "counter" button will be ineffective because we dynamically set it to static mode. But you could then add rendermode to the Counter page, the button will work again with "InteractiveServer"

    @page "/counter"
    @rendermode InteractiveServer
    ...
    

    You could also reference this document https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0#set-the-render-mode-by-component-instance


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.