razor
razor copied to clipboard
[Known Issue] Specifying a `@rendermode` fails when a component uses `@typeparam`
Known Issue
There is a known issue that the Razor compiler generates uncompilable C# when specifying a @rendermode
on a Razor component that also uses the @typeparam
directive.
Symptoms
When specifying both @rendermode
and @typeparam
in a Razor component, the user will receive error similar to CS0305: Using the generic type 'Component<T>' requires '1' type arguments.
Workarounds
A user can specify the rendermode manually via the @attribute
directive.
@attribute [type: RenderModeInteractiveServer]
EDIT: The attribute class needs to be also created as well since it did not ship in .NET after all:
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
class RenderModeInteractiveServer : RenderModeAttribute
{
public override IComponentRenderMode Mode => RenderMode.InteractiveServer;
}
Planned Fix
At this time, there is no fix planned, as it is believed to affect a small number of users. If you are experiencing this issue, please upvote this issue to help us prioritize this.
A compiler update with a clearer error is planned.
This is going to be a very common scenario: creating a component that accepts a generic type parameter where the component inside has a strict or very specific rendermode requirements too.
The Virtualize component does not seem to work because of this, as per my recent testing.
Also, the workaround does not work. It produces this error:
"The type or namespace name 'RenderModeInteractiveServerAttribute' could not be found (are you missing a using directive or an assembly reference?)".
I can't seem to find the namespace for RenderModeInteractiveServerAttribute. Is it because it doesn't exist anymore in .NET 8?
This should be given higher priority.
Just ran into this issue as well. The workaround didn't work either since RenderModeInteractiveServer
, along with the other render mode attributes, appear to have been removed in .NET 8 (source).
Just ran into this issue on .NET 8, so workaround does not work.
I think you should be able to create your own attribute with the same effect, for example:
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
class RenderModeInteractiveServer : RenderModeAttribute
{
public override IComponentRenderMode Mode => RenderMode.InteractiveServer;
}
and then the workaround should work:
@attribute [type: RenderModeInteractiveServer]
It works by creating the attribute
I think you should be able to create your own attribute with the same effect, for example:
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; class RenderModeInteractiveServer : RenderModeAttribute { public override IComponentRenderMode Mode => RenderMode.InteractiveServer; }
and then the workaround should work:
@attribute [type: RenderModeInteractiveServer]
This workaround works also in .Net 8 Blazor.
+1 I hit this issue and was surprised to see it's not planning on being fixed.
My workaround, is putting @rendermode="InteractiveWebAssembly"
in the parent component using the problematic component.
I am somewhat surprised (and disappointed) that this is not an easy fix in the .razor codegen and that it won't make it in the next release (https://github.com/dotnet/razor/issues/10193#issuecomment-2030587923). Good to know that there are workarounds for it, though.
+1
Hi, I ran into this issue with @rendermode InteractiveServer
The attribute workaround, worked.
+1 I've had the same issue a couple of times, workaround with custom attribute works in .net8