Aspnetcore: Build error when parameter and child component have the same name

Created on 7 Nov 2018  路  3Comments  路  Source: dotnet/aspnetcore

Add support for @tagHelperPrefix

Problem

Current implementation of Blazor v0.6.0 does not have support for Razor @tagHelperPrefix

Functional impact

Trying to Include a child component inside a RenderFragment of a parent component where both the child and the RenderFragment have the same name will result in compile time error.

Minimal repro steps

Let us suppose we have a component in Panel.cshtml as follows:

<div class="panel panel-default">
  <div class="panel-heading">@PanelHeader</div>
  <div class="panel-body">@PanelBody</div>
</div>

@functions{
  [Parameter] private RenderFragment PanelHeader { get; set; }
  [Parameter] private RenderFragment PanelBody { get; set; }
}

and another component in PanelHeader.cshtml like this:

<div>@Title</div>
<div>@SubTitle</div>

@functions{
  [Parameter] private string Title { get; set; }
  [Parameter] private string SubTitle { get; set; }
}

then using these two components in a page like below code will result in a compile time error:

<Panel>
  <PanelHeader>
       <PanelHeader Title="some title" SubTitle="sub title"></PanelHeader>
  </PanelHeader>
  <PanelBody>Some content ...</PanelBody>
<Panel>

Proposal

I would recommend to add support for @tagHelperPrefix so to simplify distinguishing components from RenderFragments.

Above code could then can be re-written as follows:

Panel.cshtml would be like this:

@tagHelperPrefix "pre1:"
<div class="panel panel-default">
  <div  class="panel-heading">@PanelHeader</div>
  <div  class="panel-body">@PanelBody</div>
</div>

@functions{
  [Parameter] private RenderFragment PanelHeader { get; set; }
  [Parameter] private RenderFragment PanelBody { get; set; }
}

And PanelHeader.cshtml would look like this:

@tagHelperPrefix "pre2:"
<div>@Title</div>
<div>@SubTitle</div>

@functions{
  [Parameter] private string Title { get; set; }
  [Parameter] private string SubTitle { get; set; }
}

And finally both the child and the parent components can be used like this:

<pre1:Panel>
  <PanelHeader>
       <pre2:PanelHeader Title="some title" SubTitle="sub title"></pre2:PanelHeader>
  </PanelHeader>
  <PanelBody>Some content ...</PanelBody>
<pre1:Panel>
area-blazor bug

All 3 comments

Thanks for this suggestion!

We don't plan to add support for @tagHelperPrefix because the intent is for components and parameters to follow the same name resolution rules as .NET types and properties. For example if you need to namespace qualify a component name you would simply specify the namespace in addition to type name: <Namespace.MyComponent />. This isn't implemented yet, but is tracked by https://github.com/aspnet/Blazor/issues/1315.

That said, your scenario should work today as is, and the fact that it doesn't I think is a bug. It's the equivalent of having a property and type with the same name, which should be fine.

Oh that will be great but at the same time would be very lengthy to type in all the namespaces. Perhaps, when implemented you may add a feature to provide something like using statement in C#

@fayezmm Yup, the good news is Razor already has a @using directive for that.

Was this page helpful?
0 / 5 - 0 ratings