Pingu
Pingu
CC#
Created by Pingu on 2/15/2024 in #help
Building a Blazor `RenderFragment` in base razor component to be used in derived?
Hey. So I have a base razor component that I want to derive from multiple times, as there will be a lot of duplicated code between the multiple derivations. One thing I have stumbled into, is that I have a title which is largely similar between all the derived components. For this, I currently have the following:
protected virtual string ObjectName => "Item";

protected RenderFragment TitleFragment() => builder =>
{
builder.OpenComponent<MudText>(0);
builder.AddAttribute(1, nameof(MudText.Typo), Typo.h6);
builder.AddAttribute(2, nameof(MudText.Class), "mr-10");

builder.AddAttribute(3, nameof(MudText.ChildContent), (RenderFragment) ((builder2) =>
{
builder2.OpenComponent<MudIcon>(4);
builder2.AddAttribute(5, nameof(MudIcon.Class), "mr-3 mb-n1");
if (IsCreating)
builder2.AddAttribute(6, nameof(MudIcon.Icon), Icons.Material.Filled.Add);
else
builder2.AddAttribute(6, nameof(MudIcon.Icon), Icons.Material.Filled.Edit);
builder2.CloseComponent();
if(IsCreating)
builder2.AddContent(7, $"Create {ObjectName}");
else
builder2.AddContent(7, $"Edit {ObjectName}");
}));
builder.CloseComponent();
};
protected virtual string ObjectName => "Item";

protected RenderFragment TitleFragment() => builder =>
{
builder.OpenComponent<MudText>(0);
builder.AddAttribute(1, nameof(MudText.Typo), Typo.h6);
builder.AddAttribute(2, nameof(MudText.Class), "mr-10");

builder.AddAttribute(3, nameof(MudText.ChildContent), (RenderFragment) ((builder2) =>
{
builder2.OpenComponent<MudIcon>(4);
builder2.AddAttribute(5, nameof(MudIcon.Class), "mr-3 mb-n1");
if (IsCreating)
builder2.AddAttribute(6, nameof(MudIcon.Icon), Icons.Material.Filled.Add);
else
builder2.AddAttribute(6, nameof(MudIcon.Icon), Icons.Material.Filled.Edit);
builder2.CloseComponent();
if(IsCreating)
builder2.AddContent(7, $"Create {ObjectName}");
else
builder2.AddContent(7, $"Edit {ObjectName}");
}));
builder.CloseComponent();
};
This works, and in my derived razor components I can invoke TitleFragment() and get the title that I desire. However, I wasn't sure if there was a way to create a RenderFragment in such a way without using the RenderTreeBuilder? Can I instead somehow declare this as razor syntax somewhere within my base razor component, or is this my only option?
10 replies
CC#
Created by Pingu on 1/21/2024 in #help
Blazor AuthStateProvider - OK to synchronize Role Claims?
Hey. I have a .net 8 blazor app using the "new" project style. I'm using static SSR as well as WASM. By default, the PersistingRevalidationAuthenticationStateProvider and PersistentAuthenticationStateProvider only sync userId and email claims. I've added a small amount into this as a proof of concept sort of thing, but it was non sensitive data (getting users real name). I've reached the point where I am adding roles, however. I have a page which renders some additional content when the logged in user has the Admin role (using AuthorizeView). When the page loads initially, this is visible (to the correct user) due to it rendering through static SSR. When it gets passed over to WASM however, the role claim is missing and the content disappears. I could sync the role claims across using the Auth state providers above, and am about to do so... My question is, are there any security concerns in doing this? And if so what should be my solution for achieving this instead?
1 replies
CC#
Created by Pingu on 1/6/2024 in #help
Blazor .net8 DI state sharing between client and server
No description
2 replies
CC#
Created by Pingu on 5/25/2023 in #help
❔ Blazor Project won't compile - Previously did without any changes.
14 replies