-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Razor compiler doesn't understand "@using Alias = Namespace.ComponentClass" #7670
Comments
Thanks for contacting us, @egil . |
Nope. We didn't add support for this as part of dotnet/aspnetcore#5577. We wanted to add it later based on usage and demand because adding support for this isn't trivial. |
Sounds like you have some background regarding this, @ajaybhargavb. |
Thanks for taking the time to (re)consider this. I think it will be a very useful thing to have when dealing with naming conflicts between different component libraries, and that could have a pretty big impact on the stability of our apps when we upgrade libraries or add now ones. |
+1 for this one, as the project grows I can really see the benefit of this one. |
This is definitely something we would need. I am building a multi-tenant system where I want to include a component and be able to overwrite it with a different one with the same name but in a different namespace. If I could alias all my components that would be the perfect solution to resolve the issue of having the same component name in different namespace and not being able to use both namespaces because of it. Instead of doing @using Components, we could just do a @using Header=Components.Header |
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
This is a big deal for component vendors to help developers avoid naming conflicts with native components as well as other 3rd party components. |
Thanks for contacting us. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
This is a huge issue. We can't use any external component libraries that use the namespace I can't use an alias to get around the issue and razor syntax doesn't understand I'm not sure what to do besides changing the entire namespace of our product, disregarding company policies. |
Guys, any thoughts to make this resolved? More than 2 years have passed since the original post. It would be more convenient for me and a lot of people to have aliases in razor pages - I think people expecting mostly the same behavior for razor pages as for usual cs files. Please consider to take this issue from the backlog to some sprint, thanks |
We're still looking forward to have this issue (and our classes) resolved. As a quick tip to the other suffering souls out there, a partial workaround is to subclass the 3:rd party components as local classes in your own namespace. It's not great and doesn't work for sealed classes, but it's a useful hack for now. |
This would be great for a bit of abstraction when using various libraries. For example you might be using @using Blazorise
<Button Color="Color.Primary">My Button</Button> But then you realize it would be great if your buttons got disabled when they are loading. So you inherit namespace Blazorise.Providers.MyCustomProvider
{
using Microsoft.AspNetCore.Components.Rendering;
public class Button : Blazorise.Button
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
this.Disabled = this.Loading;
base.BuildRenderTree(builder);
}
}
} But to use your custom Button, because of ambiguity you now have 2 not-so-ideal options
<CustomButton Color="Color.Primary">My Button</CustomButton>
<MyCustomProvider.Button Color="Color.Primary">My Button</MyCustomProvider.Button> Both approaches require replacement of all current occurrences of the Would be great if we could just write inside _Imports.razor using Button = Blazorise.Providers.MyCustomProvider.Button |
Another vote for this, for slightly different reasons outlined in the linked issue |
It's been a year since my last comment, but this limitation is really having a negative impact on my company's ability to provide a great experience with custom Blazor components. The only option we are really left with is to provide some silly prefix to the name of our components just to avoid any collisions. @mkArtakMSFT can we get some type of response on the current status for planning implementation of alias support? |
We'll be considering this during .NET 7 planning. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
The lack of Besides the already mentioned issues, I believe the following issues are also related
As @TLabWest mentioned back in March 2021 this scope of this issue is quite large. I think it's a mistake to label this issue as A small use-case example: MyPage.razor @namespace Contoso.UI.Blazor
@using global::Blazor.FancyButton
<div class="my-component">
This Blazor component is defined in the <strong>Contoso.UI.Blazor</strong> package.
</div>
<Blazor.FancyButton.MyButton /> MyButton.razor (for example from an external NuGet package) @namespace Blazor.FancyButton
<div class="my-component">
This Blazor component is defined in the <strong>Blazor.FancyButton</strong> package.
</div> MyPage_razor.g.cs will output the following error |
@TanayParikh @333fred It seems to be relatively easy comparing to the full aliases support, but still would allow to resolve a lot of requests related to the aliases support. Would you accept it? |
The lack of alias support can be a problem for MudBlazor. We have |
Suggestion: The compiler should store the last namespace of a Blazor component in a variable as it "steps" through the Razor page. When there is a RZ9985 error and one of the conflicting types namespace matches (or partially matches?) the current namespace component variable namespace, then that namespace should be used otherwise throw RZ9985 error. E.g.
|
Still nothing on this? I mean, having my code look like this:
Very concise. |
I come here after years of using Blazor to see that it is still not implemented. What the heck! |
This is an issue for us using multiple variants of an icon, all with the same name. For example, I want to show either a solid bell icon or an outline bell icon based on whether the user has unread notifications. Right now I have to do: @if (unread)
{
<My.Big.Long.Namespace.Components.Icons.Solid.BellIcon />
}
else
{
<My.Big.Long.Namespace.Components.Icons.Outline.BellIcon />
} It would be far more usable to be able to do: @using OutlineBellIcon = My.Big.Long.Namespace.Components.Icons.Outline.BellIcon
@using SolidBellIcon = My.Big.Long.Namespace.Components.Icons.Solid.BellIcon
@if (unread)
{
<SolidBellIcon />
}
else
{
<OutlineBellIcon />
} |
Push comes to shove, Shawn, you could look to inherit from each into your own namespace with class names of the Old/New you propose..?
|
@CaiusJard Yeah for sure, there are ways around it. Aliasing would just be a better way around it 🙂 |
Using a Razor component via an alias isn't recognized by the compiler
If I want to rename/alias a Razor component when using it in a Razor page/Razor component, I should be able to reference it using the alias syntax,
@using Alias = Namespace.ComponentClass
. However, the Razor compiler does not recognized<Alias />
as a component, and will instead generate code that treats<Alias />
as aMarkupString
.Visual Studio Razor editor doesn't recognize
Alias
as aComponentClass
component either.To Reproduce
Steps to reproduce the behavior:
.\using-rename\Pages\Index.razor
to look like this:Build using
dotnet build
. See no errors in output.See
.\using-rename\obj\Debug\netstandard2.0\Razor\Pages\Index.razor.g.cs
containsbuilder.AddMarkupContent(3, "\r\n\r\n<Renamed></Renamed>");
. Here is the relevant snippet:Expected behavior
Visual Studio should recognize
Renamed
as ausing_rename.Components.Test
component, and the compiler should output the correct C# render tree builder code.Additional context
The text was updated successfully, but these errors were encountered: