17

When using the "Publish Web Site" feature in Visual Studio 2013. How can you exclude publishing the web.config so that it doesn't overwrite the server web.config?

The question is identical to the following except the VS version.

How to exclude web.config when publishing with Visual Web Developer Express?

However, its solution does not apply to VS2013. "Build Action" option cannot be found in VS2013. And setting "ExcludeFilesFromDeployment" causes compile problems.

Community
  • 1
  • 1
Lai
  • 1,320
  • 2
  • 13
  • 24
  • Care to elaborate which compile problems you are facing when using "ExcludeFilesFromDeployment" ? – Marco May 04 '15 at 08:11
  • 1
    As Cyril Durand answered, don't. Your problem isn't how to exclude web.config but how to have two different web.config files. This is done using transformations and publishing profiles – Panagiotis Kanavos May 04 '15 at 08:14

6 Answers6

40

Simply select the web.config properties and change 'Build Action' to 'None' and 'Copy To Output Directory' to 'Do Not copy'

Abhishek Dey
  • 1,601
  • 1
  • 15
  • 38
  • I cannot find the 'Build Action' in the properties. The project is a web site project. – Lai May 05 '15 at 02:51
  • 3
    Perfect. The accepted solution is not good, once you could commit some credentials. – John John Pichler Sep 28 '18 at 19:32
  • Perhaps the functionality has changed recently, but with these two settings, 'Build Action' to 'None' and 'Copy To Output Directory' to 'Do Not copy', my deploy now deletes the web.config: `` `C:\> .\Mysite.deploy.cmd /T ` ....` ` Info: Deleting file (Mysite\Web.config).` ` ....` – David Cobb Oct 03 '18 at 16:22
  • 1
    As I'm limited to 5 minutes, I cannot edit the above comment. Intended change follows: Perhaps the functionality has changed recently, but with these two settings, 'Build Action' to 'None' and 'Copy To Output Directory' to 'Do Not copy', my deploy now deletes the web.config. How can 'leave web.config alone' be chosen as an option here? – David Cobb Oct 03 '18 at 16:28
8

I know this is an old post, with old answers but to me, both described solutions are wrong.

Web.config flavors induces a security risk when manipulating environments credentials, and the "Build Action"="None" solution, breaks the possibility to debug the project locally,as the Web.Config file will never be present in the "bin" directory.

A cleaner solution is what is described here :

https://learn.microsoft.com/fr-fr/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/excluding-files-and-folders-from-deployment

which is basically to create a ProjectName.wpp.targets file, containing a list of files / folders to exclude when publishing your project.

To remove the Web.config from a project named Blog you would need to create a file named Blog.wpp.targets with something like this:

File: Blog.wpp.targets

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ExcludeFromPackageFiles Include=".\Web.config">
      <FromTarget>Blog.wpp.targets</FromTarget>
    </ExcludeFromPackageFiles>
  </ItemGroup>
</Project>

No need to change anything in your project or .csproj file.

Miiite
  • 1,517
  • 10
  • 16
4

Excluding the web.config file is not the recommended solution. You should use web.config transformation file instead, this is the web.xxx.config file.

Example of transformation configuration file

When you publish your website, Visual Studio will merge your local web.config with the corresponding web.xxx.config file

The config transform file use XDT (XML Document Transform) syntax. For example, if you want to change your connectionstring, you can use this piece of code in you transformation file :

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyDB" 
         connectionString="value for the deployed Web.config file" 
         xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

See Web.config Transformation Syntax for Web Project Deployment Using Visual Studio for more examples and information.

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
  • I am facing two obstacles. (1) Can I have transform file for "Web.Sitemap" also? (2) My project is a website project, rather then application project. – Lai May 05 '15 at 02:54
  • 2
    @Lai 1/ There is no built-in support for web.xxx.sitemap but you can manually change your csproj to do that. Have a look at the following link http://publicityson.blogspot.fr/2011/04/visual-studio-xml-file-transformation.html. 2/ WebSite is really limited for build customization, I recommend migrating your website to a webapplication, it is a lot simpler. – Cyril Durand May 05 '15 at 07:31
  • 1
    When I wish to do anything that is not supported by MS product out-of-the-box it looks very hacky. – Lai May 05 '15 at 09:04
  • First I found msdn document hard to follow. After first try when I modified Web.Release.config and publish my website, it changed web.config as expected. – Aryan Firouzian Feb 24 '17 at 11:05
  • 3
    This is not a solution. Many web.config files contain deployment-specific secrets that should not be in source control or (potentially) even available to developers. It also does not handle multiple different deployments. – JounceCracklePop Mar 29 '18 at 23:35
  • 1
    This is a really bad solution if you have credentials on your web.config file. – John John Pichler Sep 28 '18 at 19:32
  • 3
    This is NOT what the user asked – lllllllllllllIllllIll Feb 14 '19 at 10:56
  • 1
    Can't stand it when a user responds with an answer that 1) does not answer the question at all and 2) decides to tell the OP they are wrong. – Craig Jul 25 '21 at 21:12
4

2020 and using AspCore project, then try to add below code in your .csproj:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>
  

Refer to https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2#webconfig-file

valentasm
  • 2,137
  • 23
  • 24
4

If you're publishing to Azure App Services from Visual Studio (I'm using 2019), and didn't set the web.config's Build Action to 'none', you can do this every time:

  1. Right click on your project, select publish. Then click on "Preview Changes". enter image description here

  2. Then un-check the web.config file so it does not get published. You'll get a chance to review what will be publish too.

enter image description here

I am doing this because I publish to multiple instances and each instances are of different version. This way I get to review the web.config everytime to make sure if there's any additional AppSettings configuration added, I will be aware of it.

Harvey Darvey
  • 706
  • 8
  • 16
3

For an legacy ASP.NET Web Application, add <ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment> to the appropriate .xmlpub profile file found in the Properties\PublishProfiles folder.

<Project>
    <PropertyGroup>                          
        <ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>             
    </PropertyGroup>
</Project>  


    
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
  • Exclude multiple file format; Web.config;File2.xyz;File3.xyz Exclude Folders; Folder1;Folder2;Folder3 – Must.Tek Sep 22 '21 at 09:31