razor icon indicating copy to clipboard operation
razor copied to clipboard

[Known Issue] Specifying a `@rendermode` fails when a component uses `@typeparam`

Open chsienki opened this issue 1 year ago • 22 comments

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.

chsienki avatar Dec 08 '23 21:12 chsienki

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.

nolisj avatar Dec 18 '23 08:12 nolisj

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).

david-acker avatar Jan 12 '24 23:01 david-acker

Just ran into this issue on .NET 8, so workaround does not work.

vinnyrose avatar Jan 24 '24 16:01 vinnyrose

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]

jjonescz avatar Jan 25 '24 08:01 jjonescz

It works by creating the attribute

jriveracerecer avatar Mar 01 '24 15:03 jriveracerecer

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.

kreadyf avatar Mar 06 '24 13:03 kreadyf

+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.

brad-technologik avatar Mar 30 '24 00:03 brad-technologik

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.

epsitec avatar Apr 02 '24 05:04 epsitec

+1

Hi, I ran into this issue with @rendermode InteractiveServer The attribute workaround, worked.

W4lm4s avatar Apr 02 '24 22:04 W4lm4s

+1 I've had the same issue a couple of times, workaround with custom attribute works in .net8

MRmP avatar Apr 14 '24 14:04 MRmP