1823 views

.NET Project Publish: Dynamic File Exclusion or Inclusion by Client

Summary

This article explains how to dynamically exclude or include files in a .NET project during publishing based on client parameters, enabling customized releases for multiple client websites.

When creating a publishing script for MSites projects, we can pass the client name as a publishing parameter. This allows client-specific styles, resources, icons, and even component files to be dynamically included in the final Release package.

This version only handled static resources and style files. However, for projects that include .cs files or other components, some files may be customized per client and should not be included in other clients' builds. To solve this, we can dynamically include the client-specific build configuration in the .csproj file during build.

Directory Structure Example

<Project>
  <ItemGroup>
    ...
    <Compile Remove="Views\Shared\Components\Breadcrumb\Blog.cshtml" />
    <None Remove="Views\Shared\Components\Breadcrumb\Blog.cshtml" />
    ...
  </ItemGroup>
</Project>

Client build.targets Example

Inside sites/customX/build.targets, you can list the files to exclude:



  
    ...
    
    

  

Note:

  • Compile : files that need to be compiled
  • Content : content files
  • None : other types

The easiest way to generate these entries is to manually exclude files in Visual Studio, then copy the resulting .csproj snippet to the build.targets.

.csproj Configuration Example



<project sdk="Microsoft.NET.Sdk.Web">
  <propertygroup>
    <!-- Default client -->
    <client condition="'$(Client)' == ''">default</client>
    <!--Client configuration file path -->
    <clientconfig>../../sites/$(Client)/build.targets</clientconfig>
  </propertygroup>

  <!-- Dynamically import client-specific configuration -->
  <import project="$(ClientConfig)" condition="Exists('$(ClientConfig)')">
</import></project>
  

Notes:

  • Ensure ClientConfig path is correct.
  • $(Client) value is provided as a publishing parameter.

Publish Command Example


dotnet publish -c Release -o "d:/publish/felixblog" --self-contained false --verbosity minimal -p:IsProduction=true -p:Client=felixblog
  

During build, the files listed in sites/felixblog/build.targets will be dynamically included or excluded based on the client parameter. This approach is very convenient for multi-client projects.