You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过MSBuild将.NET Framework Web应用发布至非IIS虚拟目录的普通文件夹

解决.NET Framework 4.5 Web项目MSBuild发布时的ASPCONFIG错误

问题场景

使用.NET Framework 4.5 + MSBuild CLI在未安装IIS的独立机器上构建Web应用,计划发布至普通文件夹作为可传输文件集,执行发布命令时触发以下错误:

ASPCONFIG: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

错误原因是发布目录并非IIS虚拟目录,但这符合当前环境需求,需要关闭MSBuild对目标目录的IIS应用检查。

当前使用的MSBuild命令:

& msbuild.exe MySolution.sln /t:MyProject /p:Configuration=Release /p:PublishProfile=FolderProfile /p:Platform="Any CPU" /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=true /p:publishUrl=./Publish/myProject

当前发布配置文件(FolderProfile.pubxml):

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl>C:\dev\publish\myProject</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>True</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <WDPMergeOption>MergeAllPagesAndControlToASingleAssembly</WDPMergeOption>
    <UseMerge>True</UseMerge>
    <ContentAssemblyName>Compiled</ContentAssemblyName>
  </PropertyGroup>
</Project>

解决方案

错误核心是**预编译(PrecompileBeforePublish=true)**时,MSBuild会模拟IIS应用环境进行验证,触发目录检查。根据是否需要预编译,有两种解决方式:

方式1:关闭预编译(最简单)

如果不需要预编译Web项目,直接修改发布配置或命令参数禁用预编译:

  • 修改发布配置文件中的<PrecompileBeforePublish>为False:
    <PrecompileBeforePublish>False</PrecompileBeforePublish>
    
  • 或者在MSBuild命令中添加参数:
    /p:PrecompileBeforePublish=false
    

方式2:保留预编译,禁用IIS检查

若需要保留预编译功能,添加以下MSBuild参数跳过IIS应用验证:

/p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false

修改后的完整MSBuild命令示例:

& msbuild.exe MySolution.sln /t:MyProject /p:Configuration=Release /p:PublishProfile=FolderProfile /p:Platform="Any CPU" /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=true /p:publishUrl=./Publish/myProject /p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false

也可以直接将这两个属性添加到发布配置文件的<PropertyGroup>中:

<UseWPP_CopyWebApplication>true</UseWPP_CopyWebApplication>
<PipelineDependsOnBuild>false</PipelineDependsOnBuild>

预期结果

执行修改后的命令后,MSBuild会成功将包含web.config、!app.offline等所有项目文件发布至目标普通文件夹,不会触发IIS虚拟目录检查,无错误抛出,生成可直接传输部署的文件集。

内容的提问来源于stack exchange,提问作者ksardas2008

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 12:35:22