Visual Studio 2022 doesn't like setting Build Action of a CSS file to None.

dg2k 1,386 Reputation points
2023-03-17T15:53:20.33+00:00

Visual Studio Version: Version 17.6.0 Preview 2.0 (I haven't tested on other version and may apply)

I wanted to set the Build Action property of a CSS file to None but VS doesn't like this and shows error message Property Value is not valid and excludes the CSS file from the project. I thought any file's Build Action property can be set to None, just to stay as part of the project until it is needed. I have some HTML files for note-taking with Build Action=None and the very reason why I needed the CSS file with these HTML files.

I am assuming that this could be a bug in which case I could report it to VS developers team, but thought I should pass it past Q&A just in case I am missing something, and if there is a workaround.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,700 questions
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 27,821 Reputation points Microsoft Vendor
    2023-03-31T09:51:48.42+00:00

    Hello @dg2k ,

    Thanks for your reply.

    Your reply provides the key information => .NET MAUI project.

    In short:

    In your .csproj file, add <EnableDefaultNoneItems>false</EnableDefaultNoneItems> in property group.

    Long story:

    I tested the usual web project before and the situation is quite different when I tested in .NET MAUI project. The issue you got should be a new behavior of new SDK style projects or Maui projects.

    The key is the setting in this file => C:\Program Files\dotnet\packs\Microsoft.Maui.Sdk\6.0.548\Sdk\Microsoft.Maui.Controls.DefaultItems.targets. This file is pointed in the detailed error message(the path maybe a little different). If you open the Microsoft.Maui.Controls.DefaultItems.targets file, you can see following ItemGroup:

    <ItemGroup Condition="'$(EnableDefaultItems)'=='True' And '$(EnableDefaultCssItems)'=='True' And '$(EnableDefaultEmbeddedResourceItems)'=='True'">
    		<None Remove="**\*.css" Condition="'$(EnableDefaultNoneItems)'=='True'" />
    </ItemGroup>
    
    

    When the condition is true, the ***.css file will be removed. So, let’s focus on $(EnableDefaultNoneItems). Here’s the description: EnableDefaultNoneItems.

    The EnableDefaultNoneItems property controls whether None items (files that have no role in the build process) are implicitly included in the project. The default value is true. Set the EnableDefaultNoneItems property to false to disable implicit inclusion of None items.

    Apparently, when we changed the Build Action to None, the .css file will be removed(excluded) from the Maui project and then the error appears => One or more values are invalid. Cannot add ..css to the project, because the path is explicitly excluded from the project.

    If you open the project file(.csproj), you can see there’s a new Item group added to remove the .css file and there’s no property settings for EnableDefaultNoneItems property. The EnableDefaultNoneItems property is set to true by default. So the solution is easy, add following line as a property to your .csproj file and you can successfully change the Build Action to None.

    <EnableDefaultNoneItems>false</EnableDefaultNoneItems>
    

    Have a great day.

    Sincerely,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful