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 ?

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | .NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    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. Anonymous
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.