Exclude files from web site publish in Visual Studio

Viewed 66102

Can I exclude a folder or files when I publish a web site in Visual Studio 2005? I have various resources that I want to keep at hand in the Solution Explorer, such as alternate config files for various environments, but I don't really want to publish them to the server. Is there some way to exclude them? When using other project types, such as a .dll assembly, I can set a file's Build Action property to "None" and its Copy to Output Directory property to "Do not copy". I cannot find any similar settings for files in a web site.

If the IDE does not offer this feature, does anyone have good technique for handling such files?

12 Answers

If you can identify the files based on extension, you can configure this using the buildproviders tag in the web.config. Add the extension and map it to the ForceCopyBuildProvider. For example, to configure .xml files to be copied with a publish action, you would do the following:

<configuration>...
    <system.web>...
        <compilation>...
            <buildProviders>
                <remove extension=".xml" />
                <add extension=".xml" type="System.Web.Compilation.ForceCopyBuildProvider" />
            </buildProviders>

To keep a given file from being copied, you'd do the same thing but use System.Web.Compilation.IgnoreFileBuildProvider as the type.

I think you only have two options here:

  • Use the 'Exclude From Project' feature. This isn't ideal because the project item will be excluded from any integrated IDE source control operations. You would need to click the 'Show All Files' button on the Solution window if you need to see the files in Solution Explorer, but that also shows files and folders you're not interested in.
  • Use a post-build event script to remove any project items you don't want to be published (assuming you're publishing to a local folder then uploading to the server).

I've been through this before and couldn't come up with anything really elegant.

For Visual Studio 2017, WebApp Publish, first create a standard file system publish profile. Go to the App_Data\PublishProfiles\ folder and edit the [profilename].pubxml file.

Add

<ExcludeFilesFromDeployment>[file1.ext];[file2.ext];[file(n).ext]</ExcludeFilesFromDeployment>

under the tag<PropertyGroup> You can only specify this tag once, otherwise it will only take the last one's values.

Example:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>C:\inetput\mysite</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <ExcludeFilesFromDeployment>web.config;mysite.sln;App_Code\DevClass.cs;</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>

Make sure that the tag DeleteExistingFiles is set to False

The feature you are looking exists if your project is created as a "Web Application". Web Site "projects" are just a collection of files that are thought of as 1:1 with what gets deployed to a web server.

In terms of functionality both are the same, however a web application compiles all source code to a DLL, instead of the naked source code files being copied to the web server and compiled as needed.

As a contemporary answer, in Visual Studio 2017 with a .net core site:

You can exclude from publish like so in the csproj, where CopyToPublishDirectory is never.

  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Update="appsettings.Local.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

This is discussed in more detail here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2

<PropertyGroup>
    <ExcludeFilesFromDeployment>appsettings.Local.json</ExcludeFilesFromDeployment>
</PropertyGroup>

The earlier suggestions did not work for me, I'm guessing because visual studio is now using a different publishing mechanism underneath, I presume via the "dotnet publish" cli tool or equivalent underneath.

This is just an addendum to the other helpful answers here and something I've found useful...

Using wpp.targets to excluded files and folders

When you have multiple deployments for different environments then it's helpful to have just one common file where you can set all the excluded files and folders. You can do this by creating a *.wpp.targets file in the root of the project like the example below.

For more information see this Microsoft guide:

How to: Edit Deployment Settings in Publish Profile (.pubxml) Files and the .wpp.targets File in Visual Studio Web Projects

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <EnableMSDeployAppOffline>True</EnableMSDeployAppOffline>
    <ExcludeFilesFromDeployment>
      *.config;
      *.targets;
      *.default;
    </ExcludeFilesFromDeployment>
    <ExcludeFoldersFromDeployment>
      images;
      videos;
      uploads;
    </ExcludeFoldersFromDeployment>
  </PropertyGroup>
</Project>

In Visual Studio 2017 (15.9.3 in my case) the manipulation of the .csproj-File works fine indeed! No need to modify the pubxml.

You can then construct pretty nice settings in the .csproj-File using the PropertyGroup condition, e.g.:

  <PropertyGroup Condition="$(Configuration.StartsWith('Pub_'))">
    <ExcludeFoldersFromDeployment>Samples</ExcludeFoldersFromDeployment>
  </PropertyGroup>

excludes the "Samples" folder from all deployments with configurations starting with "Pub_"...

In Visual Studio 2022 I have successfully used this settings:

  1. Go and edit the
    [ProjectName] \ Properties \ PublishProfiles \ FolderProfile.pubxml file in solution explorer.

  2. Add these lines inside PropertyGroup element:

     <ItemGroup>
         <Content Remove="Data\*.json" />
         <None Include="Data\*.json" />
     </ItemGroup>
    
  3. Then save the .pubxml file and try to publish the project.

"Content Remove" will remove the file from the content to deploy. "None Include" will keep the file in the solution explorer.

It's possible to set it up in the solution explorer for single files as well: right click the file in the solution explorer -> Properties and change the Build Action to None.

Related