Reputation: 591
It almost works after the following steps:
blazorwasm
template app.
dotnet new blazorwasm --hosted -o HostedWasm
_Hosted.cshtml
from blazorserver
template to the Pages
folder.endpoints.MapFallbackToFile("index.html")
to endpoints.MapFallbackToPage("/_Host")
in Startup.cs
of the server<script src="_framework/blazor.server.js"></script>
to <script src="_framework/blazor.webassembly.js"></script>
in _Host.cshtml
Now if you dotnet run
the server you will be able to navigate template pages with prerendered HTML.
But, I have two questions:
/
and then going to /fetchdata
everything will be fine, but if you then use F5 (e.g refresh) on /fetchdata
path, you will seeInvalidOperationException: Cannot provide a value for property 'Http' on type 'HostedWasm.Client.Pages.FetchData'. There is no registered service of type 'System.Net.Http.HttpClient'.
To fix it you need to copy HttpClient
configuration code from Client/Program.cs to Server/Startup.cs
Upvotes: 2
Views: 2156
Reputation: 80
Andrew Lock's excellent blog post, Enabling prerendering for Blazor WebAssembly apps, gives the best instructions, IMO. To repeat his steps here:
dotnet new page -o Pages -n _Host --no-pagemodel
@page
at the top@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div id="app">
with <component type="typeof(BlazorApp1.App)" render-mode="WebAssemblyPrerendered" />
builder.RootComponents.Add<App>("#app")
from Program.csservices.AddSingleton<HttpClient>
.He goes on to link to other blog posts which debate the 'right' to handle the missing HttpClient problem. I think just injecting a client is the 'right' approach. It's a simple solution. The app is only running on the server for a few seconds. The pre-rendered app isn't interactive anyway, so it's not like the user is going to click around a bunch.
Upvotes: 1