使用CLI发布ASP.NET Core应用时,指定发布配置文件未删除目标目录文件
解决dotnet CLI发布ASP.NET Core应用不清理目标目录的问题
问题场景
使用Visual Studio创建的FolderProfile发布配置,在VS内发布时会自动清理目标目录,但执行以下CLI命令发布时,仅完成发布操作,不会清空目标目录:
dotnet publish "myproject.csproj" /p:Configuration=Release /p:PublishProfile=FolderProfile
解决方案
1. 命令行强制指定清理参数
直接在publish命令后添加/p:DeleteExistingFiles=true参数,强制开启目标目录清理:
dotnet publish "myproject.csproj" /p:Configuration=Release /p:PublishProfile=FolderProfile /p:DeleteExistingFiles=true
即使发布配置文件中已配置该属性,手动指定可避免CLI读取配置时的遗漏问题。
2. 检查并修正发布配置文件
找到项目目录下的Properties/PublishProfiles/FolderProfile.pubxml文件,确保其中包含<DeleteExistingFiles>True</DeleteExistingFiles>配置节点,且未被其他条件覆盖:
<Project> <PropertyGroup> <!-- 其他发布配置项 --> <DeleteExistingFiles>True</DeleteExistingFiles> <PublishDir>你的发布目录路径</PublishDir> </PropertyGroup> </Project>
保存文件后重新执行publish命令即可。
3. 发布前手动清理目录
如果上述方法无效,可在发布前手动执行目录清理命令:
- Windows环境(CMD):
rmdir /s /q "你的发布目录路径" dotnet publish "myproject.csproj" /p:Configuration=Release /p:PublishProfile=FolderProfile - Linux/macOS环境:
rm -rf "你的发布目录路径" dotnet publish "myproject.csproj" /p:Configuration=Release /p:PublishProfile=FolderProfile
内容的提问来源于stack exchange,提问作者mxmissile
相关产品推荐
相关产品推荐

