admin管理员组文章数量:1356077
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
});
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
});
Share
Improve this question
edited Apr 1 at 23:56
Zhi Lv
22k1 gold badge27 silver badges37 bronze badges
asked Mar 31 at 11:23
PizzaOverflowPizzaOverflow
1981 silver badge6 bronze badges
1 Answer
Reset to default 1var 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.
本文标签:
版权声明:本文标题:Using Blazor for generating static serverless website (without .NET server and without .NET client) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743951387a2567353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论