Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Using Blazor for generating static serverless website (without .NET server and without .NET client)

$
0
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 questionSome 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});

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>