问题
When I publish my Asp.Net MVC website to the production server (via VS2008), the web.config & castle.xml files are overwritten. The content of these files is obviously different between my local dev environment and the production server.
How do I prevent these files from being published?
回答1:
In Visual Studio solution explorer, go to your web.config file's properties. Make sure "Build Action" is "None" and "Copy to Output Directory" is "Do not copy".
If you ever want to update it in the future you'll have to do it manually or change "Build Action" back to "Content". The next time you build (or publish) it will overwrite it.
回答2:
I solved it by adding this exclusion:
<ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>
to the project file (.csproj or .vbproj), inside these nodes:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
and
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
See Web Deployment: Excluding Files and Folders via the Web Application’s Project File.
回答3:
Never publish directly from Visual Studio to a production server!
Publish to a testing server for QA to look at, and then copy from QA to production (for an existing web site with no iis changes, it's just a matter of copying files).
Where I'm at, we also keep these published files under revision control separately, and so when QA signs off on a build the deployment process is simply checking out the files from source control on the production server.
回答4:
We use a Jenkins pipeline to deploy .Net Core/Framework apps to an ISS server.
One of the steps defines a Power Shell script that calls MSBuild with the specified project and publish profile.
Adding the ExcludeFilesFromDeployment tag to the Publish Profile script worked.
WebDeploy-Staging.pubxml:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- All the other tags excluded for brevity -->
<ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>
</PropertyGroup>
</Project>
</Project>
来源:https://stackoverflow.com/questions/2812905/how-do-i-prevent-publish-web-from-overwriting-config-files