By default when publishing a Web project using MSBuild/Visual Studio the config transforms are applied.
I would like to include the config transforms within the output.
Input
web.config
web.Debug.config
web.Release.config
Default Output
web.config
Desired output
web.config
web.Debug.config
web.Release.config
Update the files Build Action to Content using Visual Studio (e.g. right click, properties)
The Publish tasks will still transform the files, so we need to tell MSBuild, that we do not want to transform those files when publishing.
This can be achieved by passing the following parameters into MSBuild:
/p:ProfileTransformWebConfigEnabled=false /p:MarkWebConfigAssistFilesAsExclude=false
If you are working within Visual Studio, you test this behavior by adding these properties to a folder publish profile PublishProfile.xml
<!-- Disable Web.config Transforms -->
<ProfileTransformWebConfigEnabled>false</ProfileTransformWebConfigEnabled>
<MarkWebConfigAssistFilesAsExclude>false</MarkWebConfigAssistFilesAsExclude>
Include web.release.config in Web Deploy output
By default, when publishing a website, VS does not package web.debug.config and web.release.config but only the web.config.
To achieve what you want, you can add a custom target into publishprofile.pubxml to include these extra files.
Please try this:
<Target Name="CustomCollectFiles">
<ItemGroup>
<AdditionFiles Include="xxxxxxxxxxx\Web.Debug.config;xxxxxxxxx\Web.Release.config">
</AdditionFiles>
<FilesForPackagingFromProject Include="%(AdditionFiles.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
And then you will find these files in the Publish folder when you finishing Publish step.
Hope it could help you.
I was using Azure Dev Ops Server, and I wanted to run the Release transform on build, but also have the possibility to run additional transforms per pipeline target. In my case to change the SessionDb connection string
I added /p:MarkWebConfigAssistFilesAsExclude=false to the build parameters
I set my web.Prod.config to <CopyToOutputDirectory>Always</CopyToOutputDirectory>
I was still getting a NullReference exception when doing the transform. I had to remove
<compilation xdt:Transform="RemoveAttributes(debug)" />
from the prod config transform because that property was removed with the release config transform.
I am trying to exclude a folder in publish profile using ExcludeFoldersFromDeployment but it is not wxcluding while publishing to azure-app service
folder location : a\b\c\foldername
Please let me know if anyone can help!!
Add an additional answer, in case someone got confused by different EXCLUDE methodologies, if you are working in this environment:
Visual Studio 2017 + ASP.NET core 2 + Azure App Service.
try this:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.1#exclude-files
Edit YourProject.csproj file,
<ItemGroup>
<Content Update="wwwroot/content/**/*.txt" CopyToPublishDirectory="Never" />
</ItemGroup>
BTW, I also tried these posts, which do not work:
use .wpp.targets file: Excluding Files and Folders from Deployment
Use Web Deploy Profile: Exclude unwanted binaries from azure package
Use ExcludeFilesFromDeployment or ExcludeFoldersFromDeployment: Web Deployment: Excluding Files and Folders via the Web Application’s Project File
By all those methods, web.config could not be excluded, even following this post:
How to exclude web.config when publishing with Visual Studio 2013?
According to this article, you need to edit the .pubxml or the .wpp.targets file and add an ExcludeFilesFromDeployment element or an ExcludeFoldersFromDeployment element (or both) in the PropertyGroup element. Check whether you have correct setting as the example shows:
<PropertyGroup">
<ExcludeFilesFromDeployment>
File1.aspx;File2.aspx
</ExcludeFilesFromDeployment>
<ExcludeFoldersFromDeployment>
Folder1;Folder2
</ExcludeFoldersFromDeployment>
</PropertyGroup>
Another option to exclude files or folders is to use the PublishIgnore NuGet package. This option is explained in Web Publishing a simpler way to exclude files/folders from being published on the .NET Web Development and Tools blog.
In order to exclude folders from a Blazor hosted app (like a PWA) you need to add the exclusion to the Client project, not to the Server.
I.e., you have a solution with a project named Server and a project named Client.
The Client.csproj should contains the exclusion, like:
<ItemGroup>
<Content Update="wwwroot\images\contents\**" CopyToPublishDirectory="Never" />
</ItemGroup>
Now, the wwwroot folder created by the Server.pubxml will not contains the images/contents folder and sulfolders.
I was also struggling with the issue and after looking at the #Dongdong answer I came to following conclusion:
Edit YourProject.csproj file and include the following
<ItemGroup>
<Content Update="web.Debug.config;web.Release.config;web.QA.config;appsettings.Debug.json;appsettings.Release.json;" CopyToPublishDirectory="Never" />
</ItemGroup>
You can also apply some condition as well.
<ItemGroup>
<Content Condition="'$(Configuration)|$(Platform)'=='QA|AnyCPU'" Update="appsettings.json;" CopyToPublishDirectory="Never" />
</ItemGroup>
You can extend it as per your requirement. Excluded files from the directory won't be published.
I've recently introduced NUnit to a Visual Studio C# project. The project folder structure looks like
- project root
-- applications (rich client interface, web interface, small tools)
-- components (business logic)
-- vendor (3rd party components)
-- tests (Nunit tests)
For each Visual Studio project "MyProject" under applications or components there is a corresponding project under tests named "MyProject.Test". When I introduced the NUnit test, I put the following in each .Test.csproj file:
<Target Name="AfterBuild">
<CreateItem Include="$(TargetPath)">
<Output TaskParameter="Include" ItemName="MyProjectTests" />
</CreateItem>
<!-- Create folder for test results -->
<MakeDir Directories="$(OutDir)\TestResults" />
<!-- Run tests-->
<NUnit Assemblies="#(MyProjectTests)" ToolPath="..\vendor\NUnit\bin" OutputXmlFile=".\TestResults\MyProject.Test.Results.xml" WorkingDirectory="$(OutDir)" />
<!-- Create HTML report -->
<Xslt Inputs="$(OutDir)\TestResults\MyProject.Test.Results.xml" Xsl="$(MSBuildCommunityTasksPath)\NUnitReport.xsl" RootTag="Root" Output="$(OutDir)\TestResults\MyProject.Test.Results.html" />
</Target>
This works fine, both when building solutions from within Visual Studio as well as on a build server with the MSBuild CLI.
The remaining inconvenience of that approach is that it leaves me with the test reports in a TestResults folder in each test projects output folder, but with nothing in my solution's main output folder. So, my question is:
What is the preferred way of collecting the resulting NUnit html reports in the solution's/startup project's output folder? What MSBuild instructions should I place in which .csproj file? I'm just getting started with MSBuild and I can't figure out the best practice...
It has to work both in Visual Studio and with the MSBuild CLI, but that shouldn't be a problem, I guess.
Thanks
If you have a build server, one good approach is to setup a site with a virtual path configured to your test folder. Then any person on your company can browse something like http://build.companydomain.local/yourapp/nunitreports/, hosted on the build server's IIS (in case your build server have one).
I'm doing this with my coverage reports. Any person can browse it at any moment. I hope it helps!
In the team I'm working in we have a big product with many WCF web services and some web sites which use the services. We are just about to upgrade to VS 2010 and I'm looking at if we should start using the new config transform functions in VS 2010.
We have several different environments which need different web.configs (database connection strings, WCF addresses and so on). Often when debugging something high up such as the web frontend it is useful to configure it to directly connect with the TEST or QA backend / databases. On each developer's local machine the IIS is configured directly to the source folder of each WCF/web project, and when running locally it is a simple matter of Ctrl-Shift-B or F5 to debug something.
One would think that it would be possible to build/F5 with TEST or QA as configuration mode and get the TEST/QA config, but I don't see how. Is it not supported, or maybe we need to change how we work with things?
Our other option is to instead use a simple replace-script as a prebuild event that creates the web.config from a template and a key-file depending on configuration mode. With this method you would get TEST config if you compile in TEST and so on but it feels a bit bad to roll our own solution when there is a function built into Visual Studio.
You can achieve the effect you're looking for by using the BeforeBuild and AfterBuild targets available in the .csproj file. The VS.NET IDE will execute these targets when doing a Build or a Rebuild, so you can use them to execute the web.config transforms. Since you'll need to do a web.config transform and then overwrite the actual web.config file, you'll need to rely on a new file called web.default.config to store the base web.config data.
I tried this out in a test project, here were the changes I made to the .csproj file:
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<ProjectExtensions>
...
</ProjectExtensions>
<Target Name="BeforeBuild">
<Copy SourceFiles="$(ProjectDir)web.default.config" DestinationFiles="$(ProjectDir)web.config" />
</Target>
<Target Name="AfterBuild" Condition="$(FirstRun) != 'false'">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="TransformWebConfig" Properties="FirstRun=false;" />
<Sleep Milliseconds="2000" />
<Copy SourceFiles="$(ProjectDir)obj\$(ConfigurationName)\TransformWebConfig\transformed\web.config"
DestinationFiles="$(ProjectDir)web.config" />
</Target>
I had to manually add these to the .csproj file (I used Notepad++). As far as I can tell there is no way to add these instructions through the VS.NET IDE. You need to supply the conditional on the AfterBuild to keep from having a circular reference, as the call to MSBuild will rerun the build to generate the web.config transform.
Basically what we're doing is copying the web.default.config file (our base template) over the existing web.config before we start to build, and then we use MSBuild to generate a web.config for whatever configuration we're building. After the transform is complete, we use a Copy task to take the transformed file and copy it over to the web.config file in the web root. One issue I occasionally ran into was a file in use error when trying to overwrite the web.config after the transform was complete. Adding a Sleep task (from MSBuildCommunityTasks) after the MSBuild task took care of that issue.
I only tested this approach using the built in ASP.NET server, not IIS, so YMMV but I feel like this is a workable solution.
The FirstRun idea came from this post.
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?
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
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:
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.
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 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.
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.
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
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.
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.
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:
Go and edit the
[ProjectName] \ Properties \ PublishProfiles \ FolderProfile.pubxml file
in solution explorer.
Add these lines inside PropertyGroup
element:
<ItemGroup>
<Content Remove="Data\*.json" />
<None Include="Data\*.json" />
</ItemGroup>
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.