Monthly Archives: March 2014

Including Files in the Visual Studio Publish Process

Including Files in the Publish Process

Visual Studio has the facility to publish the web application to File System, FTP, Web Deploy, Azure, etc. This is fantastic feature, as it does many thing on your behave, like Web.config transformation, removing *.cs files, *.designer.cs files, etc. which are not required to run the application.

In MVC application, it remove controller, model folder as it just contains the *.cs file, which are build in YourWebApp.dll.

Folder structure is clean, but there is one caveat: if you application requires certain extra files to publish, then, it won’t automatically do it for you.

There are two ways you can do it,

Option 1: Either includes your custom file in VS project, and set the Build Action to “Content”

Option 2: Long way

Option 1 was easier, but I land in the soup, for the resource file. Resource file are embedded resource, and goes with the WebApp.dll. So far, so good, because, any resource string you required in your application, it would be provide by the ResourceManager.


But, in my project, there was custom helper method in my project, with would read the resource file.

What the problem?

Well, Visual Studio publish won’t publish resource file to my publishing directory. I need some way, to tell publisher to copy resource file (*.resx) whenever you are publish the web application.

Ten-ta-ten, I quickly google and found the way at Microsoft own site,

http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/deploying-extra-files

But, the example given on the site, was slight different from my requirement.

<Target Name=”CustomCollectFiles”>

<ItemGroup>

<_CustomFiles Include=”..\ExtraFiles\**\*” />

<FilesForPackagingFromProject Include=”%(_CustomFiles.Identity)”>

<DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>

</FilesForPackagingFromProject>

</ItemGroup>

</Target>

Now, I need resource file to be copied, and I quickly change to following

<Target Name=”CustomCollectFiles”>

<ItemGroup>

<_CustomFiles Include=”..\Content\ResourceFiles\TrainingCoursesResources.resx/>

<FilesForPackagingFromProject Include=”%(_CustomFiles.Identity)”>

<DestinationRelativePath>Content\ResourceFiles\TrainingCoursesResources.resx</DestinationRelativePath>

</FilesForPackagingFromProject>

</ItemGroup>

</Target>

But, it didn’t work. Reason, I screwed up the relative path for the resx file.

I got the help from following links

http://stackoverflow.com/questions/14775587/relative-paths-with-msbuild-project-vs-solution

http://stackoverflow.com/questions/17031299/definecustomfiles-and-customcollectfiles-are-not-fired-by-teamcity

Problem is ‘..\Content\’ tell MSBuild to look into folder, which is above my web root directory.

I fixed the issue, by removing ‘..’ and hence, all solved

<Target Name=”CustomCollectFiles”>

<ItemGroup>

<_CustomFiles Include=”Content\ResourceFiles\TrainingCoursesResources.resx/>

<FilesForPackagingFromProject Include=”%(_CustomFiles.Identity)”>

<DestinationRelativePath>Content\ResourceFiles\%(Filename)%(Extension)</DestinationRelativePath>

</FilesForPackagingFromProject>

</ItemGroup>

</Target>

Advertisements