NonUnique
NonUnique
CC#
Created by NonUnique on 5/22/2024 in #help
HTML renders before/during OnInitializedAsync - Blazor server side (no SignalR)
Hi, I want to use Blazor to return static html with no .NET interactivity so SignalR is disabled. this is Test.Razor
@page "/test"
<h3>Test</h3>
@foreach (var line in lines) @*<- lines not set yet*@
{
<p>@line</p>
}
@code {
private List<string>? lines = null;
protected override async Task OnInitializedAsync()
{
await Task.Delay(1000);
lines = ["1", "2"];
await base.OnInitializedAsync();
}
}
@page "/test"
<h3>Test</h3>
@foreach (var line in lines) @*<- lines not set yet*@
{
<p>@line</p>
}
@code {
private List<string>? lines = null;
protected override async Task OnInitializedAsync()
{
await Task.Delay(1000);
lines = ["1", "2"];
await base.OnInitializedAsync();
}
}
Task.Delay would be my real IO call. Issue is that HTML C# portion starts executing before lines is being set. So it errors out on foreach. https://blazor-university.com/components/component-lifecycles/ it's here early in lifecycle. If I add @if (lines is not null) it returns properly but does it mean that in backend it keeps redrawing on changing values until OnInitializedAsync is fully done? If I need to handle each value arriving from async methods as possibly null then I sort of make my code work with no data and on real bad data I wouldn't not get 500 to indicate bad data. For RazorComponentResult in minimal API responses I suppose I could do async methods and pass results as parameters but then all logic is lost from .razor page. Now as I think this might enable child/parent communication before returning HTML so it's intended and I should always assume anything IO will be missing initially (to be aware in child parameters or in parent should be covered with if before making render child component)? Maybe there is some true static server side mode? I'm very new to Blazor but have sense how this would be useful on SPA/SignalR streams.
2 replies
CC#
Created by NonUnique on 11/8/2022 in #help
Setting response code for SPA static file to 404?
Hi, I have a problem - I need ngsw.config missing file to return 404 instead of SPA 200 with index.html. I tried it setting with OnPrepareResponse but apparently you cannot change status code there? SPA site is hosted on IIS. How can I make non existing file ngsw.config return 404 status code? Startup.cs part (the one if(context.File.Name.StartsWith("ngsw.json"))):
// Client
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}

app.UseSpa(spa =>
{
spa.Options.SourcePath = "../ClientApp";

spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
// force rechecking Angular's index.html to prevent using index.html that references old scripts
if (context.File.Name == "index.html")
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
context.Context.Response.Headers.Add("Pragma", "no-cache");
context.Context.Response.Headers.Add("Expires", "0");
}

// to get rid of running service workers return 404 on cache bust as fail-safe deactivation
if (context.File.Name.StartsWith("ngsw.json"))
{
context.Context.Response.StatusCode = 404;
}
},
};

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
// Client
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}

app.UseSpa(spa =>
{
spa.Options.SourcePath = "../ClientApp";

spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
// force rechecking Angular's index.html to prevent using index.html that references old scripts
if (context.File.Name == "index.html")
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
context.Context.Response.Headers.Add("Pragma", "no-cache");
context.Context.Response.Headers.Add("Expires", "0");
}

// to get rid of running service workers return 404 on cache bust as fail-safe deactivation
if (context.File.Name.StartsWith("ngsw.json"))
{
context.Context.Response.StatusCode = 404;
}
},
};

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
1 replies
CC#
Created by NonUnique on 10/11/2022 in #help
Serilog loses List-object- property values when serializing
Anyone know fix to my issue? https://stackoverflow.com/questions/74030184/serilog-loses-listobject-property-values-when-serializing >Problem: Property List<object> gets destructed into [[[[]],[[]]]].
1 replies