C
C#4d ago
d4shm1r

What is missing or what is the proper instructions I should follow?

I created a new project using dotnet new blazor -o MyWebApp and at basic level at a page called Alpha.razor inside Pages inside Components file when I create a simple function private void test(){Console.WriteLine("Hello World");} and call it with <button @onclick="test">Test</button> However in the debugging session or when I run the project in the localhost:5345/Alpha I can't see the message Hello World after I click the button unless I add in the first line of Alpha.razor @rendermode InteractiveServer. It looks ok but the problem arises in latter steps when I include @rendermode InteractiveServer on shared pages like NavMenu.razor to do similar tasks it doesn't work like it works on pages. My question again is what should I exactly do to fix the functionality part.
3 Replies
Jimmacle
Jimmacle4d ago
you need to decide where your C# code needs to run and select a render mode based on that, or support all potential render modes that may be used there are various combinations of webassembly (client side), server (server side), plus server prerendering that can happen if you expect to see the output of the C# code in the server console, then that will only happen if the component is being rendered server side interactively as you've already found for my purposes (small internal webapps) i just globally set the project to be server interactive without prerendering by setting the render mode of Routes like:
<Routes @rendermode="new InteractiveServerRenderMode(prerender: false)"/>
<Routes @rendermode="new InteractiveServerRenderMode(prerender: false)"/>
d4shm1r
d4shm1rOP4d ago
Honestly that might be the cause of problem root identification something like globally rendering. Here is my final Routes.razor tweak and it works like magic: <Router AppAssembly="typeof(Program).Assembly"> <Found Context="routeData"> <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> <FocusOnNavigate RouteData="routeData" Selector="h1" /> </Found> </Router> @rendermode InteractiveServer I just added @rendermode InteractiveServer in the bottom there and now it works.
Jose Rizal
Jose Rizal3d ago
set the interactive render mode globally instead of adding @rendermode in each component like navmenu

Did you find this page helpful?