Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Excluding Files and Folders in Visual Studio Web Site Project


:P
On this page:

This post is a self-reminder, so I can find this information in the future: I still use quite a few Web Site Projects in Visual Studio for very simple, all static sites. Most of my product sites, info pages and a host of small client side samples are all simple apps that don't require any fancy 'DevOps' deployment mechanism, and in these cases Web Deploy often offers a simple solution to quickly get content updated on live site.

For example, in my current requirement, I have a completely static Angular project that sits on its own dedicated static site and communicates with an external API endpoint.

I don't even use Visual Studio for the Angular development (WebStorm all the way) but other parts of the project live in Visual Studio and deploy to a separate server. So the Web Site project is just one other project that's published. In this case the 'site' is the final output from an Angular ng build command in it's own dedicated project folder, which is part of the larger Solution as a Web Site project.

Web Site projects and simple Web Deploy often make a good solution and Web Deploy is one of the easiest and quickest ways to publish from Visual Studio when you're not dealing with .NET code in a project.

Yes I know - push button deploy is often frowned upon. But for many static sites like my product sites or as in this case a small static site project that is code managed and staged through other mechanisms often don't warrant a dev ops setup and for those types of scenarios push button Web Deploy still remains a good solution.

Excluding files

One thing that invariably happens is that there is one or more dynamic files in a project that shouldn't be deployed to the server. They are needed to run the application locally (and on the server), but the file(s) is specific to the particular application installation. So they need to be excluded when the project is published and updated on an as needed basis for specific installations.

And of course I forget the syntax to do this every single time!

In this specific setup the Angular application has a local configuration file appConfiguration.json that's dynamically loaded at runtime when the Angular application starts (via an APP_INITIALIZER) and holds site specific configuration settings so each deployed site will have its own copy and this file shouldn't be deployed.

Web Site Projects store the Deployment information in a App_Data/PublishProfiles folder in a .pubxml file for each Publish Profile.

The specific .pubxml file settings that can be used to exclude files and folders are:

<Project>
    <ProjectGroup>
        <!-- Exclude Files -->
        <ExcludeFilesFromDeployment>appConfiguration.json;*.pubxml</ExcludeFilesFromDeployment>
        
        <!-- Exclude Folders -->
        <ExcludeFoldersFromDeployment>temp;otherfolder</ExcludeFoldersFromDeployment>
    </ProjectGroup>
</Project>

Simple enough.

Now when I publish the appConfiguration.json file is not published to the server. Simple solution - once you know the keys to set.

Problem solved.

Wait, where are my Web Site Projects, Dude?

Several people asked about Web Site projects in Visual Studio as Microsoft has done a good job hiding this project type. The reason most people think it's no longer available is because Web Site Projects are not shown on the Visual Studio Start Screen. But they are still available in the main Visual Studio menu.

If you're starting Visual Studio from scratch you have to use Continue without code:

You can then use either File → Open → Web Site (when there's no solution open), or if you have an existing Solution File → Add → Existing Web Site:

Can we use .NET SDK Projects? Sort of, not really?

While I was mucking around with this I was once again thinking about how to avoid Web Site Projects. These projects have many problems like not excluding folders like node_modules which can make client side projects maddeningly slow, and for not working in non-Visual Studio for Windows environments.

It would seem that a .NET SDK project could offer the same features for content files only. While on the surface Web Site Projects which don't need a project file at all seem simpler, the fact that you use the common format used for other projects in an SDK project, plus all the nice filtering features that are not easily available in a Web Site project.

SDK projects have an easy way to specify what gets included and excluded, plus it's more familiar than the .pubxml syntax that I will never remember and not readily find when I'm looking for it in a search (maybe now with this post I'll at least know where to look )

It's possible to set up a pseudo content project like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
  </PropertyGroup>

  <!-- Include all files -->
  <ItemGroup>
    <None Update="**/*.*">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <!-- add specific exclusions -->
  <ItemGroup>
    <None Update="appConfiguration.json">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
    <None Update="**/*.pubxml">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
    <None Update="node_modules/**/*.*">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None> 
  </ItemGroup>
</Project>

This does work but there are a couple of problems with SDK Projects in a static site scenario:

  • Require an Entry Point (ie. static void Main())
  • Require a TargetFramework
  • Produce binaries that can't be excluded

If you can live with extra deployed files (ie. the binaries) you can fix the first point by explicitly adding a program.cs file:

namespace PraMobile {
    public class PraMobileTemp
    {
        public static void Main(string[] args)  { }
    }
}

This makes the project build even though it literally does nothing. Unfortunately, it also creates a few small .dll,.pdb and .exe which do also get deployed and I can't see a way to exclude them via project file options.

So that's the downside - you get junk on the server that you don't want there.

Summary

It sure sure would be nice if there was a new style SDK project that didn't have a compilation target. With that we could finally get rid of Web Site projects and use the 'no target' project for content only projects and still get the 'project' features like publishing. But I bet that would be a hard sell for the project system, given the .NET moniker :

Today Web Site projects still work for this, but they don't fit well into .NET Core solutions and there still is no really good alternative.

I hope that's something Microsoft will address in the future as I find myself with lots of uses for these types of static file only content projects.

Resources

this post created and published with the Markdown Monster Editor
Posted in ASP.NET  Visual Studio  

The Voices of Reason


 

Alexandre Konioukhov
July 26, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

but I think we don't have Web Site Project in VS 2019 anymore. Or I miss something?


John Marsing
July 27, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

Have you looked at Azure Static web apps?


Rick Strahl
July 27, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

@Alexandre - yes it's still there, but it's not on the start screen. If you're starting VS up use continue without code. At the main window use File → Open → Web Site. Or if you have a solution open Add → Existing Web Site.


Rick Strahl
July 27, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

@John - you're missing the point. I don't want to use Azure!. And even if I did it wouldn't change how the project is managed in Visual Studio.


Jon
August 05, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

I've requested content-only project types several times over the years. On that horrible VS suggestion site it typically gets ignored and auto-closed, and as a project-system github issue it was closed with the usual "not planned" canned response.

There are many scenarios where that project type would be useful. Another common need I often have is to store and organize SQL scripts. Documentation comes to mind. And instead I end up using the incredibly clumsy pseudo-folders at the Solution level.


Rick Strahl
September 14, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

@Jon - For purely static content Solution folders work. VS also has a new Folder Project which is basically just a file reference. That might work for your scenario of scripts. The problem with that project type is that it's literally just a folder dump - there's no support for publishing and other project type features which still would be useful.

SDK projects already provide all the features needed if we could just do away with requiring an executable and entry point for the project that would be all that's needed.


jp2code
November 20, 2020

# re: Excluding Files and Folders in Visual Studio Web Site Project

I see how to remove a file, but how do I remove a FOLDER? We have a TypeScript folder in our project that has 10 files and 14 Subfolders. Each subfolder has files and folders as well. If I could just tell VS2019 to exclude Folder "TS" from the build, that would save LOTS of time!


PK
February 04, 2021

# re: Excluding Files and Folders in Visual Studio Web Site Project

ExcludeFilesFromDeployment not working in ASP.NET Core. I don't understand why! My publish config below:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\netcoreapp3.1\publish\</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ProjectGuid>62c12bf8-a83f-4907-90c0-fcc4f5550c46</ProjectGuid>
    <SelfContained>false</SelfContained>

    
    <ExcludeFilesFromDeployment>TestFile.txt</ExcludeFilesFromDeployment>

  </PropertyGroup>
</Project>

Any help, please.


Ian
February 18, 2021

# re: Excluding Files and Folders in Visual Studio Web Site Project

FYI Rick the code snipped showing files and folders syntax has some typos.

  • It shows a folder opening tag (<ExcludeFoldersFromDeployment>) and a file closing tag (</ExcludeFilesFromDeployment>).
  • The surrounding tag says <ProjectGroup> but should be <PropertyGroup>

Excellent resource dude, thanks.


AlMo
February 27, 2022

# re: Excluding Files and Folders in Visual Studio Web Site Project

Publish profiles now seem to be located under Project Properties (the little wrench icon, not the Properties page). Took 4-eva to find em, too; why does MSFT make the stupidest simplest repetitive task so hard to find in help/bugs/etc.? At least as of 16 and 17 versions of VS.


Rick Strahl
March 04, 2022

# re: Excluding Files and Folders in Visual Studio Web Site Project

Yeah that bugs me too. The profiles have moved several times in recent versions and to make things worse, VS 2022 can't read profiles directly: It creates .pubxml files, but when you try to import a profile it looks for .publish-settings which are different. The only way this works is via a setting in the .csproj.user file that points at an existing .pubxml file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <NameOfLastUsedPublishProfile>.\Properties\PublishProfiles\IISProfile.pubxml</NameOfLastUsedPublishProfile>
  </PropertyGroup>
</Project>

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024