Reputation: 352
In a Blazor WebAssembly .NET Core hosted application, after changing the {PROJECT NAME}.Client project name to "{PROJECT NAME}.Admin" (changing the client project name), scope identifies for CSS isolation in the {PROJECT NAME}.Admin.styles.css file and rendered objects in the DOM are different.
I created a .NET core hosted Blazor WebAssembly application with authentication. Ran the application. Worked fine.
Then I changed the project name of {PROJECT NAME}. Client project to "{PROJECT NAME}.Admin". And changed basically everywhere there was "Client" to "Admin".
Then I added the project references.
When I ran the project... This was the landing page.
All the functionality works fine. All the components are rendered. But only Bootstrap styling is applied to the MainLayout and NavMenu components as they use CSS isolation. The isolated CSS files generated files scope identities aren't the same as what is rendered to the DOM.
Image of {PROJECT NAME}.Admin.styles.css file
The page renders fine if I change the scope Identities manually. But it resets when I Run the application the next time.
I'm just playing around with Blazor. So I'm expecting a fix to this problem rather than some alternate method or workaround to do the styling. I'm new to online forums. Sorry for providing unnecessary information or providing less information. Please request any additional information needed.
Upvotes: 11
Views: 6264
Reputation: 176
Same thing happened to me, and I trawled a lot of StackOverflow queries that suggested cleaning and rebuilding would sort it, which it didn't.
Then I realised that there must be something about the blazor compiler only rebuilding when it thinks the original sources have changed.
So I opened /Layout/NavMenu.razor.css
and Layout/MainLayout.razor.css
, added a blank line and saved them. Then the publish worked - it must have checked the modified-dates on these files and decided that it didn't need to regenerate the scoped versions. Changing that modified time caused it to re-transpile them.
Note that you might have other transpiled CSS files (likely native Blazor ones, rather than your own components) that you may need to touch
to force than trans-compilation.
Upvotes: 1
Reputation: 1179
I spent many hours on the same issue. In my case it was sufficient to use "Clean" on the project (in Visual Studio: right click on the project, and select the "Clean" option).
It has the same effect as deleting the bin and obj folders as mentioned in another answer.
Upvotes: 4
Reputation: 391
I had the same issue. Rebuilding the solution fixed it for me. Possibly rebuilding just the relevant application would have been enough too.
Upvotes: 3
Reputation: 201
Another workaround (not a solution as it should already "just work") is mentioned under "Customize scope identifier format" in: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-5.0 .
Basically add something like this to your csproj file:
<ItemGroup>
<None Update="Pages/Example.razor.css" CssScope="my-custom-scope-identifier" />
</ItemGroup>
If you provide a specific scope identifier like this and ensure you clear your browser cache, it seems to force the scope identifier that gets bundled in the {AssemblyName}.styles.css to match what gets rendered in the html
Upvotes: 2
Reputation: 875
What worked for me was deleting the obj
and bin
folders, besides renaming the link to the css file to {AssemblyName}.styles.css.
Setting it to ProjectName does not work if it differs from AssemblyName.
This way it was not necessary for me to delete and recreate the specific css files. Note: I'm using Blazor Server with ASP.NET 5
Upvotes: 9
Reputation: 352
Deleting the CSS isolated files and creating them again from the beginning made it work! How ever rebuilding the solution or cleaning the solution did not work. Still no clue of what was going on.
Upvotes: 13