More than ten years later, but I did find a solution. There is still no "true" extern alias
in Razor, but C# 10 did introduce global using
, which together with namespace aliases can be used as a workaround.
Technically, the statement go anywhere, but the entry point of the application is the most natural place to put it. So, in Program.cs
or App.cs
or the equivalent, at the top of the file put
extern alias MyAlias;
global using __MyAlias = MyAlias::My.Alias;
Now, all C# files, including files auto-generated by Razor, will be able to use __MyAlias
in place of MyAlias::My.Alias
. Unfortunately, a using
alias can't then be used in a new using
statement, so you'll have to do this with every fully-qualified-namespace in the colliding assembly.
The other alternative is to split your C# code from your .razor
file into a separate .razor.cs
file, where you can use C# normally; but this won't let you import components from an aliased assembly.
Unfortunately, I have to retract this answer, as upon further testing it only appeared to fix the issue. In particular, it doesn't in fact extend to component .razor
files, which are the most crucial. (In all other cases we can just make a code-behind file and use extern alias
normally). Perhaps this false start might help someone else find a solution, so I'm leaving it up.