Exclude files from web site publish in Visual Studio
Asked Answered
H

12

86

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?

Hinda answered 16/3, 2009 at 15:18 Comment(2)
Here you can find a very simple solution: #1289358Parlin
Here is the solution for VS 2019: https://mcmap.net/q/213075/-asp-net-core-exclude-or-include-files-on-publishVernice
S
34

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.

Sebi answered 16/3, 2009 at 15:35 Comment(1)
Super cool. Was having no luck with the recommended ExcludeFromPackageFiles in the wpp.targets file. The latter is fairly useless as it doesn't seem to support wildcards with folders more than one level deep. ThanksMezuzah
W
141

Exclude files and folders by adding ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment elements to your project file (.csproj, .vbproj, etc). You will need to edit the file in a text editor, or in Visual Studio by unloading the project and then editing it.

Add the tags anywhere within the appropriate PropertyGroup (Debug, Release, etc) as shown below:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment>
    <ExcludeFilesFromDeployment>**\.svn\**\*.*</ExcludeFilesFromDeployment>
    <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment>
</PropertyGroup>

Wildcards are supported.

To explain the example above:

  • The 1st ExcludeFilesFromDeployment excludes File1.aspx (in root of project) and Folder2\File2.aspx (Folder2 is in the root of the project)
  • The 2nd ExcludeFilesFromDeployment excludes all files within any folder named .svn and any of its subfolders
  • The ExcludeFoldersFromDeployment excludes folders named Folder1 (in root of project) and Folder2\Folder2a (Folder2 is in the root of the project)

For more info see MSDN blog post Web Deployment: Excluding Files and Folders via the Web Application’s Project File

Wycliffite answered 14/7, 2013 at 9:31 Comment(11)
Is there a way to user wildcards with this?Heinrich
@Heinrich Yes, wildcards are supported.Wycliffite
This is much better than the accepted answer for me as I want to exclude the Web.config file during Publish!Rabble
On "Web Site" type projects, this code snippet gets added to the *.pubxml files located in ~/App_Data/PublishProfiles/.Robin
In a web application you can add these elements to the PropertyGroup of you publish profile @ ~\Properties\PublishProfilesScrewball
This answer is quite good, it covers so much more than what the other answers do. I had a project that published all files in project folder, but I needed to exclude a few folders and this solution did the job perfectly.Priscian
Is there any way to do this via the UI rather than editing the project file directly?Wyatan
Perfect for skipping then BIN folder when publishing a TypeScript projectPert
Question is about web sites, not web apps.Gatekeeper
Worked like a charm! This is exactly the kind of thing I was looking for, and it still works today, thanks!Euphrates
Would this method work for excluding files from deletion on server on publish?Murat
Y
43

Amazingly the answer for Visual Studio 2012 is not here:

  • The answer with green checkmark is not the answer.

  • The highest "upped" answer references an article from 2010 and says you have to edit your csproj project file which is now incorrect. I added the ExcludeFoldersFromDeployment XML element to my Visual Studio 2012 csproj file and it did nothing, the element was considered invalid, this is because ExcludeFoldersFromDeployment has been moved to the .pubxml file it looks like.

For Web Applications and Websites you edit the .pubxml file!

You can follow my answer or try this guide which I found later: http://www.leniel.net/2014/05/using-msdeploy-publish-profile-pubxml-to-create-an-empty-folder-structure-on-iis-and-skip-deleting-it-with-msdeployskiprules.html#sthash.MSsQD8U1.dpbs

Yes, you can do this not just for Website Projects but Websites too. I spent a long time on the internet looking for this elusive exclude ability with a Visual Studio Website (NOT Website project) and had previously concluded it was not possible but it looks like it is:

In your [mypublishwebsitename].pubxml file, found in ~/Properties/PublishProfiles for Web Application Projects and ~/App_Data/PublishProfiles for Websites, simply add:

  <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment> 
 <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment>

as children to the main <PropertyGroup> element in your .pubxml file. No need to add a new element not unless you are keying a specific build type, like release or debug.

BUT WAIT!!!

If you are removing files from your destination/target server with the following setting in your Publish configuration:

enter image description here

Then the Web Publish process will delete on your source/target server anything excluded, like an item you have delineated in your <ExcludeFoldersFromDeployment> and <ExcludeFilesFromDeployment>!

MsDeploy Skip Rules to the rescue:

First, Web Publish uses something other than MSBuild to publish (called Task IO or something like that) but it has a bug and will not recognize skip rules, so you must add to your .pubxml:

<PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
</PropertyGroup>

I would keep <WebPublishMethod> in its own <PropertyGroup>, you would think you could just have one <PropertyGroup> element in your .pubxml but my Skip Rules were not being called until I moved <WebPublishMethod> to its own <PropertyGroup> element. Yes, crazy, but the fact you need to do all this for Web Publish to exclude and also not delete a folder/file on your server is crazy.

Now my actual SkipRules, ExcludeFolders and ExcludeFiles declarations in my .pubxml:

<ExcludeFoldersFromDeployment>Config</ExcludeFoldersFromDeployment>
<ExcludeFoldersFromDeployment>Photos</ExcludeFoldersFromDeployment>
<ExcludeFoldersFromDeployment>Temp</ExcludeFoldersFromDeployment>
<ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>
 <AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest>

And now a the Skip Rules (<Target> is a child of <Project> in your .pubxml): (You may be able to leave <SkipAction> empty to Skip for all actions but I didn't test that and am not sure.

  <Target Name="AddCustomSkipRules">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
      <MsDeploySkipRules Include="SkipConfigFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\\Config</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipPhotosFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\\Photos</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipWebConfig">
        <SkipAction>Delete</SkipAction>
        <ObjectName>filePath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\\Web\.config</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipWebConfig">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\\Temp</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>

And please, do not to forget to escape the . in a filePath Skip rule with a backslash.

Yablon answered 17/4, 2015 at 0:33 Comment(0)
S
34

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.

Sebi answered 16/3, 2009 at 15:35 Comment(1)
Super cool. Was having no luck with the recommended ExcludeFromPackageFiles in the wpp.targets file. The latter is fairly useless as it doesn't seem to support wildcards with folders more than one level deep. ThanksMezuzah
C
11

I struggled with the same issue and finally pulled the trigger on converting the web site to a web application. Once I did this, I got all of the IDE benefits such as build action, and it compiled faster to boot (no more validating web site...).

Step 1: Convert your 'web site' to a 'web application'. To convert it I just created a new "web application", blew away all the files it created automatically, and copied and pasted my web site in. This worked fine. Note that report files will need to have their Build Action set to "Content" instead of "none".

Step 2: Now you can set any files "Build Action" property.

Hope this helps.

Conoid answered 12/4, 2011 at 14:13 Comment(0)
L
8

In Visual Studio 2013 I found Keith's answer, adding the ExcludeFoldersFromDeployment element to the project file, didn't work (I hadn't read Brian Ogden's answer which says this). However, I found I could exclude a text file when publishing in Visual Studio 2013 by just setting the following properties on the text file itself:

1) Build Action: None

2) Copy to Output Directory: Do not copy

Initially I tried setting the Copy to Output Directory property by itself but that didn't work when the Build Action was set to the default value, Content. When I then set the Build Action to None the text file was no longer copied to the destination folder when I published.

To view these properties in the Visual Studio GUI, in the Solution Explorer right-click on the file you want to exclude and select Properties from the context menu.

Loudermilk answered 28/7, 2015 at 21:51 Comment(2)
This works in Visual Studio 2015 publishing to Azure.Guile
Problem is when you need files excluded only for certain target environments (Build Configurations). This is painful.Subset
D
5

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.

Disqualification answered 16/3, 2009 at 15:30 Comment(0)
T
4

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

Transcendentalistic answered 9/6, 2018 at 17:19 Comment(1)
Nico are you from GP? Worked for MCI?Veron
S
3

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://learn.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.

Slangy answered 28/1, 2019 at 15:28 Comment(0)
B
1

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.

Backplate answered 16/3, 2009 at 15:28 Comment(0)
A
1

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>
Amulet answered 4/12, 2019 at 23:39 Comment(0)
D
1

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.

Damask answered 1/7, 2022 at 17:17 Comment(0)
J
0

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_"...

Jemina answered 10/1, 2019 at 10:53 Comment(1)
I verify that this works for Visual Studio 2017 without applying the "Condition" property.Obregon

© 2022 - 2024 — McMap. All rights reserved.