0

Short question: I try to store the HTML output of a Blazor to a file in order to have a static website, but I don't find the file. What am I missing?


Long question Some time ago I asked a question about generating a static serverless website with Blazor. (Use Blazor for generating static serverless website (without .NET server)) At that time, I was still wondering whether to use Blazor WASM or some generator for a pure HTML page. Now that I'm finishing my website, I'm sure I want pure HTML. I think the Blazor library takes too long to load and doesn't scroll nicely.

I've been trying to implement this in the ./Program.cs file. I've come across this blog which does a similar thing:(Andrew Lock - .NET Escapades: Rendering Blazor components to a string) Only I want don't want to serve the HTML immediatly. I want to store it as a file. That part doesn't work. I run the Program.cs. I see HTML is generated. I store it to a file. File.Exists(path) returns true. I can't find it. Even Blazor doesn't find it the next time I run it. What am I missing?

using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Be.M.Dev.TestProject;
using Microsoft.AspNetCore.Components.Web.HtmlRendering;
using Microsoft.AspNetCore.Components;
using Be.M.Dev.TestProject.Core;
using System.IO;


var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<HtmlRenderer>();

var app = builder.Build();

var htmlRenderer = app.Services.GetRequiredService<HtmlRenderer>();

await htmlRenderer.Dispatcher.InvokeAsync(async () =>
{
    var output = await htmlRenderer.RenderComponentAsync<Be.M.Dev.TestProject.Components.HomeComponent>(ParameterView.Empty);
    var html = output.ToHtmlString();

    var path = Path.GetFullPath("index.html");
    Console.WriteLine(File.Exists(path)); // OUTPUT false, even after rerunning
    await File.WriteAllTextAsync(path, html);
    Console.WriteLine(File.Exists(path)); // OUTPUT true, but I can't find it
});

1 Answer 1

1
var builder = WebAssemblyHostBuilder.CreateDefault(args);
...
  
   await File.WriteAllTextAsync(path, html);

When you write a file in Blazor WebAssembly then it is written to memfs, an in-memory filesystem. It is not persisted.

You can try to put your page and the HtmlRenderer code in a Serverside app so that you can write to your local drives.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.