Blazor, .NET 8 and MudBlazor – starter project structure

photo of motocross dirt bike
Photo by Rodolfo Clix on Pexels.com

A very short one today…

I’ve been playing around with the new Blazor rendering modes introduced with .NET 8, figured I’d do a quick post with an example project on GitHub here for anyone who needs a quick, simple, bare bones starter project structure for Blazor, .NET and MudBlazor

Ok, what’s the problem…

grayscale photography of woman touching her eyes
Photo by Juan Pablo Serrano on Pexels.com

All seems to work fine, although due to the new rendering modes I’ve only come across one issue so far personally. Most of my older (pre .NET 8) Blazor and MudBlazor apps use the system preference to determine dark or light mode, but this is only available from the browser via JavaScript. Consequently using the default settings for server side rendering of the layout you’ll see a delay whilst it renders light mode, then once the client side is ready it picks up that its dark mode and noticeably flicks over from light to dark mode (or vice versa if the system preference is reversed and you’ve defaulted the theme to dark).

As such I’ve changed the ‘App.razor’ line for ‘Routes’ to this so the app ‘acts’ like the old ASP.NET hosted model with webassembly. If this isn’t an issue for you, just remove the rendermode attribute part and away you go…

<Routes @rendermode="new InteractiveAutoRenderMode(false)" />
the interior of a mercedes benz car
Photo by jay pizzle on Pexels.com

I’ve added a simple dashboard to the home page and a search bar on the main app bar. Apart from that, I’ve also refactored the folder structure using ‘Feature’ folders instead of the default folder structure. So this example is showing two things I suppose.

What do you think? Feature folders, or the classic folder structure?

%*?!£$ing authentication!!!

One last thing, so the new .NET Blazor is supposed to have improved authentication, which is kind of true in most cases, however anyone using Azure AD (or bloody ‘Microsoft Entra’, why??) and also Azure AD with B2C is in for a problem. The old Microsoft Identity method people used previously now will not work (see this). Also, they’ve not provided a template for this in Visual Studio like for previous versions, but said they ‘may’ provide one (see here), but when .NET 9 is released!?! Due to all this, they have provided a sample project though (see here), but its nowhere near as ‘clean’ as the old Microsoft Identity method used in previous versions.

I’ll do another post on this eventually (although there are many other post on this out there), but essentially you’ve got to maintain the authentication across the server and client if using the new .NET 8 setup. It’s a faff and not straightforward like previous versions, but you can take some of the existing code they’ve implemented and with some refactoring get it to work.