Ok so I'm stuck trying to set configure a Maui Blazor Hybrid App, something I'm quite new to.
I have some views which need to be customized for some build configurations. I'm therefore looking to selectively exclude razor pages under certain build conditions via the .csproj file, so that I can use the same route in different razor pages and only one of them ends up in each build, automatically resulting in the correct view being selected based on the files present.
I've checked Microsoft's documentation regarding multi-targeting in .NET Maui, which describes a way to exclude .cs
files based on an <ItemGroup>
item condition in the .csproj
file.
I've now set up a small test project and tried to exclude razor pages using a similar method, but it doesn't work. I'll get an error telling me the routes are ambiguous, as both files are being included in the build, meaning the <Compile Remove>
clearly isn't working. The error is the following:
The following routes are ambiguous:
'' in 'MyApp.Components.Pages.Home_Dupe'
'' in 'MyApp.Components.Pages.Home'
at Microsoft.AspNetCore.Components.RouteTableFactory.DetectAmbiguousRoutes(TreeRouteBuilder builder)
at Microsoft.AspNetCore.Components.RouteTableFactory.Create(Dictionary`2 templatesByHandler, IServiceProvider serviceProvider)
at Microsoft.AspNetCore.Components.RouteTableFactory.Create(List`1 componentTypes, IServiceProvider serviceProvider)
at Microsoft.AspNetCore.Components.RouteTableFactory.Create(RouteKey routeKey, IServiceProvider serviceProvider)
at Microsoft.AspNetCore.Components.Routing.Router.RefreshRouteTable()
at Microsoft.AspNetCore.Components.Routing.Router.Refresh(Boolean isNavigationIntercepted)
at Microsoft.AspNetCore.Components.Routing.Router.RunOnNavigateAsync(String path, Boolean isNavigationIntercepted)
at Microsoft.AspNetCore.Components.Routing.Router.<>c__DisplayClass76_0.<RunOnNavigateAsync>b__1(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
I believe this is because the actual content being displayed is found in the _razor.g.cs
files generated by the build process. I've then tried excluding those generated files with a <Compile Remove>
, which also doesn't work. Most likely because they are not even part of the project, but I had run out of ideas at that point.
Here is the relevant part of my current .csproj
file, which at this point is just trying to remove the second razor file from the build at all:
<ItemGroup Condition="true">
<Compile Remove="**\*.Dupe.razor" />
</ItemGroup>
And here is the corresponding snippet I used in an attempt to remove the _razor.g.cs
file generated during the build:
<ItemGroup Condition="true">
<Compile Remove="**\*_Android_razor.g.cs" />
</ItemGroup>
I understand that in the 2nd snippet, at least the path is wrong, but is there even a way of accessing these files via a relative path in the .csproj
file? Would removing these files in this fashion work if the path was correct? My knowledge of how razor pages are built is very very limited.
Being out of ideas, I've also tried <Content Remove>
instead of <Compile Remove>
, which also doesn't work and results in the same error mentioned above, but does remove the file from Visual Studios Solution Explorer.
Is there some established way of excluding razor pages from a build based on the pages file names via the .csproj file?
For anyone who comes across this question and cares for a solution, I now found a work-around in this github repo. It's apparently a (not-so-well) known issue in .NET MAUI with the WebView SDK.
So, answering my own question, it's currently not possible to exclude them via the .csproj file. It is possible to exclude them from the build however.