Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I stop Visual Studio 2012+ from ever publishing packages.config and EF diagram files?

When I publish an ASP.NET Web Application in Visual Studio 2012/2013/2015, Visual Studio will also publish the packages.config (from NuGet) and any *.edmx.diagram files (from Entity Framework), by default.

I know that I can go into the project and individually switch the properties for these files from Build Action Content to Build Action None, however, I have to do this for each project that I use Entity Framework, or any other NuGet-provided package.

I can configure the publishing process to exclude files on a project-by-project basis, but is it possible to tell Visual Studio 2012/2013/2015 globally to exclude these files, across all projects?

like image 802
James Skemp Avatar asked Jul 16 '15 16:07

James Skemp


1 Answers

You can exclude files by extension or filename modifying properties in the web.config file. Look at the documentation for the buildproviders element in web.config. You can add the extension and map it to the System.Web.Compilation.ForceCopyBuildProvider, or add the filename and use the System.Web.Compilation.IgnoreFileBuildProvider

The buildproviders section has the following structure:

<buildProviders> 
   <add />
   <clear/>
   <remove />
</buildProviders>

You can also exclude files or folders by modifying your project file. Within a PropertyGroup element, you can add ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment elements to exclude the desired items.

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

See the answers to this question for more details: Exclude files from web site publish in Visual Studio

UPDATE: In answer to the revised requirement that you be able to do this globally, across all projects and solutions, I'd suggest that you create a build target file that you could import into your project files. Within the target file, you can identify the file and folder exclusions you want to make.

To make it easier to deliver this build target to all of your solutions, you could create a NuGet package that contains the target file and modifies your .csproj files accordingly.

This is essentially the approach used by SlowCheetah to extend the web.config transform process to other .config files; the NuGet package delivers a custom .targets file that extends the build process.

The initial setup is an effort, but if you're supporting a lot of solutions or teams, it might be the best approach.

like image 105
dthrasher Avatar answered Nov 14 '22 23:11

dthrasher