How do I prevent ‘Publish Web’ from overwriting config files?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 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.

Method 3

  1. Add this to project file (*.csproj):
<PropertyGroup>
  <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>
  1. Delete your web.config OR:

Select web.config and change in Properties window:

    "Copy to Output Directory"="Do not copy"

Method 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>

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x