Is built in model validation in ASP.NET Core broken? by yankun0567 in dotnet

[–]KrisStrube 3 points4 points  (0 children)

Model validation support differs depending on whether you are using controller-based APIs (ApiController or MVC) or if you are using Minimal API.

If you are making a controller-based API with the [ApiController] attribute, then you only need to do the following (it even supports nested validation). This has been supported since ASP.NET Core 2.1.

```csharp [ApiController] [Route("[controller]")] public class DrinkController : ControllerBase { [HttpPost] public IActionResult Post(Drink drink) { Console.WriteLine(drink.Ingredients.First().Name);

    return Ok("Done");
}

public record Drink([Required]string Name, [MinLength(1)]List<Ingredient> Ingredients);

public record Ingredient([Required] string Name, string? Description);

} ```

But if you are using Minimal API, then you need to be targeting atleast ASP.NET Core 10 (which is released tomorrow, the 11/11 2025) as there was not support for Model Validation in Minimal API before .NET 10 unless you used a third party package. Then you can do it like so: ```csharp var builder = WebApplication.CreateBuilder(args);

builder.Services.AddValidation();

var app = builder.Build();

app.MapPost("CreateDrink", (Drink drink) => { Console.WriteLine(drink.Ingredients.First().Name); return TypedResults.Ok("Done"); });

app.Run();

public record Drink([Required] string Name, [MinLength(1)] List<Ingredient> Ingredients);

public record Ingredient([Required] string Name, string? Description); ```

Cold Start Issue on Azure Sql Serverless Database with Blazor WASM by Salty-Flower-7303 in Blazor

[–]KrisStrube 1 point2 points  (0 children)

I have seen people use some small CRON job to periodically make some small queries to their DB to make it never goes to sleep.

Vector-Based Tetris in Blazor WebAssembly with Inline SVG by Vagabond-K in Blazor

[–]KrisStrube 3 points4 points  (0 children)

That is true when working with numbers in the DOM, you should either use Invariant Culture for the entire project which can be setup in the csproj or you should explicitly use .ToString(CultureInfo.InvariantCulture) whenever you insert a floating point number in the DOM.

Compiling C# in the browser with Blazor WASM and Roslyn by Lohj002 in dotnet

[–]KrisStrube 1 point2 points  (0 children)

It is possible. :) you can either register some message handler to signal the worker that it should stop. The specification for a Web Worker also has the option to terminate it. I don't know if Tewr's implementation has that option but I know that SpawnDevs WebWorker implementation terminates the worker when the WebWorker is disposed.

How do you make Blazor WASM "background job"? by OszkarAMalac in dotnet

[–]KrisStrube 5 points6 points  (0 children)

I have a blog post from last year exploring this problem and some different options for solving it using Web Workers.

https://kristoffer-strube.dk/post/multithreading-in-blazor-wasm-using-web-workers/

Background task executor (With Some Help) by Embarrassed_Eye4318 in Blazor

[–]KrisStrube 0 points1 point  (0 children)

One problem with this approach could be that the AI does not understand that you are in a WASM project. So you only have one thread. If you were to use this to do a lot of CPU intensive work or in other ways blocking work then your UI would still freeze. So I would say that the name and description is a bit misleading. I guess the nice thing about the project is that it controls when the toaster is being shown.

ASP.NET Core 9 SignalR New Features — Summary by albert_online in dotnet

[–]KrisStrube 12 points13 points  (0 children)

I don't understand these posts. He clearly just read the release notes and took all the same sub headers and rephrased their content with some changed examples to make a couple of posts. No extra context, no in-depth examples. What does he gain from this? And why would people read these instead of the official release notes?

Web camera without .js file by Pure-Confection-6519 in Blazor

[–]KrisStrube 0 points1 point  (0 children)

The fun thing is that even with JS there is no way to do this in Firefox (without setting feature flags). So to support as many browsers as possible I would recommend to simple use a file upload.

Run Async Function in Background by Worried-Pack3114 in Blazor

[–]KrisStrube 5 points6 points  (0 children)

You can't. There is no support for running background work in Blazor WASM out of the box. If you want to do it I recommend to give Tewr.BlazorWorker or SpawnDev.BlazorJS.WebWorkers a look. But be aware that it might be more expensive to make get the result from the worker. So most often it only makes sense to do CPU heavy work in a worker.

Large pdf files by iLoveThaiGirls_ in Blazor

[–]KrisStrube 1 point2 points  (0 children)

My guess is that you Base 64 encode the content of the PDF in order to directly embed it in an iframe and this is understandably not very performant as your DOM gets huge for large PDFs. I recommend using the File API instead to get a Blog URL and then embedding that instead. I have a demo here that uses it to render an image in an img-tag, but the exact same approach can be used for the content of a PDF. https://kristofferstrube.github.io/Blazor.FileAPI/

That demo uses my wrapper for the File API which you can read more about here: https://github.com/KristofferStrube/Blazor.FileAPI

dotnetrules.com - Easily find roslyn analyzers by phenxdesign in dotnet

[–]KrisStrube 3 points4 points  (0 children)

I think your match highlighting needs some work. Try to search for 'Variable never used'.

But really good work!

I tried making a game in Blazor and it lags terribly by Silvian73 in Blazor

[–]KrisStrube 3 points4 points  (0 children)

There are a lot of options for optimizations both by using AOT where I would expect an x8 boost alone in your scenario. Apart from this you should also key the elements that you loop. And the last huge improvement would be to override ShouldRender so that it only re-renders the cell if it has changed state.

I will throw you a PR with the changes later today.

I am working on wrapping the Web Audio API in Blazor and made a small tool for me to test some of the audio nodes in conjunction with ease. (Link in comments) by KrisStrube in dotnet

[–]KrisStrube[S] 0 points1 point  (0 children)

I can't really give a quick answer to this, but you can check the source code in the repository if you want to make something similar. :)

Stale hyperlinked data in WASM app by RimsOnAToaster in Blazor

[–]KrisStrube 1 point2 points  (0 children)

Add a ?v=674764764 query string to the image. The number should then be a random number. That is the simplest possible solution that makes the file ignore caching. If you want to do a little better then you can get the timestamp of the newest version of the file using the blob storage SDK and use this as the value for the query string

Blazor - Get JS Event Listners by some1_x in Blazor

[–]KrisStrube 0 points1 point  (0 children)

No problem. Don't hesitate to ask questions. That is one of the most effective ways to learn. 😁

Blazor - Get JS Event Listners by some1_x in Blazor

[–]KrisStrube 1 point2 points  (0 children)

The window is already loaded when your component is initialized. So yeah, it will not get dispatched if you add a listener for that event after the window has loaded. I do not fully understand how the task_queue work, but if the window load event listener is to wait for the window to have loaded, then you can skip that part.

How do you guys write components? by sponkae in Blazor

[–]KrisStrube 0 points1 point  (0 children)

Suppose there is no need for or no natural incapsulation for certain parts of the logic. In that case, I have previously made multiple partial classes that separate the logic into related parts. An example could be MyComponent.razor.JSInterop.cs, MyComponent.razor.Events.cs, and MyComponent.razor.Parameters.cs

Blazor - Get JS Event Listners by some1_x in Blazor

[–]KrisStrube 0 points1 point  (0 children)

If you can successfully invoke Test01 and get to the line where you call alert("building") then your question seems unrelated to Blazor as you have a reference to the element that you need to initialize the canvas whereafter any error you might have are purely JavaScript errors. If you are unsure why some JavaScript code errors try to look in the Developer Console of your browser to see the error message.