razor: [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.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 41
- Comments: 26 (6 by maintainers)
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.
I think you should be able to create your own attribute with the same effect, for example:
and then the workaround should work:
Please consider upvoting the issue instead of commenting “+1”. When sorting issues by most upvoted (which we might do to decide what to prioritize), these comments don’t have any effect.
+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.This workaround works also in .Net 8 Blazor.
Got this issue quite a few times now.
I’ve been hit by this today on .Net 9, wasted most of a day on it. The work-around is working though, which is a relief.
It’s very disappointing that there’s no plan to fix, I’m surprised that this is considered a low priority. Generics seem essential for a flexible component system.
If Microsoft can not be persuaded to resolve this, then can I suggest at least adding some details of this known issue to your documentation, e.g. ASP.NET Core Razor component generic type support.
I’ve had the same issue, but in my case I wanted to set the prerender attribute as false. Usually, this is the way to go:
@rendermode @(new InteractiveServerRenderMode(prerender: false))But that wasn’t working due to the
@typeparamdirective being used, so I tried tweaking the workaround @jjonescz mentioned, like this:Unfortunately, that didn’t work at all. Luckily, I solved it by setting prerender to false in the parent component, but I wish I could do it to the component itself.
+1 I’ve had the same issue a couple of times, workaround with custom attribute works in .net8
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.
Just ran into this issue on .NET 8, so workaround does not work.
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).