将.NET Framework项目升级为SDK样式后遇NETSDK1022错误及运行问题
用Microsoft Upgrade Assistant将目标框架为.NET Framework 4.7.2的ASP.NET MVC项目升级为SDK样式(未修改目标框架)后,出现以下错误,同时项目的OutputType设置无法让项目像非SDK样式那样正常调试运行:
Error NETSDK1022 Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultContentItems' property to 'false' if you want to explicitly include them in your project file. The duplicate items were: 'Views\Web.config'; 'Web.config'; 'Web.Debug.config'; 'Web.Release.config';...
生成的项目初始代码片段如下:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <OutputType>Exe</OutputType> <TypeScriptToolsVersion>4.3</TypeScriptToolsVersion> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <EnableDefaultItems>false</EnableDefaultItems> <PlatformTarget>AnyCPU</PlatformTarget> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <OutputPath>bin\</OutputPath> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <Optimize>false</Optimize> <OutputPath>bin\</OutputPath> </PropertyGroup> <ItemGroup> <Reference Include="System.Data.OracleClient" /> <Reference Include="System.Net" /> <Reference Include="System.Security" /> <Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceProcess" /> <Reference Include="System.Transactions" /> <Reference Include="System.Configuration" /> </ItemGroup> <ItemGroup> <Compile Update="Models\ConnectionHistory.cs"> <DependentUpon>devDeptModel.tt</DependentUpon> </Compile> ...
解决方案
1. 修复NETSDK1022重复Content项错误
- 移除显式重复Content项:打开项目文件,找到包含
Views\Web.config、Web.config等文件的<Content>条目组,删除这些显式声明。SDK样式项目默认会自动扫描并包含目录下的Content类型文件,无需手动添加。 - 保留显式声明时禁用默认规则:如果必须保留项目文件中的显式Content条目,确保
EnableDefaultContentItems属性设置为false(你的项目已配置此属性,但需检查是否存在冲突配置)。优先推荐前一种方案,符合SDK项目的设计规范。
2. 解决OutputType导致的调试运行异常
ASP.NET MVC属于Web应用程序,SDK样式项目中无需设置OutputType为Exe,直接删除<OutputType>Exe</OutputType>这一行即可。Web项目SDK会自动处理正确的输出类型,错误设置为Exe会导致调试运行行为不符合预期。
额外优化建议
- 清理冗余系统引用:SDK样式项目会自动引用.NET Framework核心程序集,
System.Net、System.Configuration等手动添加的引用可以从<Reference>项组中删除,避免冗余。 - 调整EnableDefaultItems:若将
EnableDefaultItems改为true,可依赖SDK默认的文件包含规则,大幅简化项目文件;若保持false,则需确保所有必要的编译文件、资源文件都已显式声明。
内容的提问来源于stack exchange,提问作者abenci

