Lai
Lai

Reputation: 1350

How to exclude web.config when publishing with Visual Studio 2013?

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.

Upvotes: 19

Views: 28412

Answers (6)

James Lawruk
James Lawruk

Reputation: 31337

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>  


    

Upvotes: 4

Harvey Darvey
Harvey Darvey

Reputation: 714

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.

Upvotes: 5

valentasm
valentasm

Reputation: 2372

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

Upvotes: 6

Miiite
Miiite

Reputation: 1557

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.

Upvotes: 8

Cyril Durand
Cyril Durand

Reputation: 16192

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.

Upvotes: 5

Abhishek Dey
Abhishek Dey

Reputation: 1639

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

Upvotes: 42

Related Questions