如何在无.sln/.csproj文件时用CMD执行MsBuild构建网站项目
Great question! The difference between Web Site projects and Web Application projects is critical here—Web Sites are file-system based and don’t rely on a .csproj or .sln file, which is why Visual Studio can load and build them directly without those artifacts.
The good news is you can absolutely build a Web Site directly with MSBuild using only its root directory path—you just need to explicitly specify the right targets and parameters, since MSBuild doesn’t automatically infer the Web Site context like Visual Studio does. Here’s how to do it:
Basic Build Command
To compile your Web Site and output the built files to a specified directory, use this command:
%windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe C:\Projects\MyWebsiteRoot /t:Build /p:Configuration=Release /p:OutDir=C:\MyWebsiteBuildOutput\
Publish Command (For Deployment)
If you want to package the site for deployment (like Visual Studio’s "Publish" feature), use the Publish target instead:
%windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe C:\Projects\MyWebsiteRoot /t:Publish /p:Configuration=Release /p:PublishDir=C:\MyWebsitePublish\ /p:WebPublishMethod=FileSystem
Key Parameter Breakdown
Let’s break down what each part does:
\t:Build: Triggers the core compilation of the site’s code-behind files and copies all necessary assets (HTML, CSS, config files) to the output directory.\t:Publish: Packages the entire site for deployment, including optional precompilation of code-behind into assemblies (more on that below).\p:Configuration=Release: Uses the Release build configuration (swap withDebugif you need a debug build).\p:OutDir/\p:PublishDir: Specifies the target directory where your built or published files will be saved.\p:WebPublishMethod=FileSystem: Tells MSBuild to publish to a local directory (you can also use methods likeFTPorWebDeployif needed).
Useful Optional Parameters
- Precompile Before Publish: Add
\p:PrecompileBeforePublish=trueto the Publish command to precompile code-behind files into assemblies, which can improve runtime performance:%windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe C:\Projects\MyWebsiteRoot /t:Publish /p:Configuration=Release /p:PublishDir=C:\MyWebsitePublish\ /p:WebPublishMethod=FileSystem /p:PrecompileBeforePublish=true - Fixed Assembly Names: By default, Web Sites compile each code-behind file into a unique random-named assembly. Add
\p:FixedNames=trueto use consistent, predictable assembly names (e.g.,App_Code.dllinstead ofApp_Code.XXXX.dll):%windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe C:\Projects\MyWebsiteRoot /t:Build /p:Configuration=Release /p:OutDir=C:\MyWebsiteBuildOutput\ /p:FixedNames=true
Important Notes
- Match MSBuild Version to Your .NET Framework: The example uses
v4.0.30319for .NET 4.0. If your site uses .NET 4.5+, use the corresponding MSBuild version from%windir%\Microsoft.NET\Framework\v4.5.x\(e.g.,v4.5.2usesmsbuild.exein%windir%\Microsoft.NET\Framework\v4.5.2\). - No Single Assembly Output: Unlike Web Application projects (which produce a single project assembly), Web Sites compile code-behind files into multiple assemblies in the
bindirectory (unless you use precompilation with fixed names).
内容的提问来源于stack exchange,提问作者Muhammad Musavi

