Deploying Content files depending on the target project kind (web, windows, dll, ...)

Jun 2, 2011 at 8:00 AM


Is there a way to decide if a file in the "Contents" folder should be included or not in the target project, depending on if it is a Web application, Windows Form, or just a library?

Well, this is my scenario: I have 3 projects:

  1. "DataAnnotations" project, DLL with custom localized data annotations attributes
  2. "DTO" project, DLL with the models, which uses my custom data annotations
  3. "Web" project, an MVC web app, which uses the models at DTO, and registers the custom data annotations providers from my custom Data Annotations

"DataAnnotations" and "DTO" are deployed as NuGet packages. 

  • DTO uses DataAnnotations package
  • Web uses DataAnnotations package and DTO package

In order to change the default client validation provider, I need to override them in the global.asax with the ones from my DataAnnotations project, like this:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredLocalizedAttribute), typeof(RequiredLocalizedAttributeAdapter));

But, following David Ebbo's recomendations, I decided to use WebActivator to leave Global.asax untouched, so it's easier to add new adapters as they come without changing it. So, I included a file in the package at Content/App_Start/InstallCustomDataAnnotations.cs.pp with the following code:

using System.Web.Mvc;
using Custom.DataAnnotations;

[assembly: WebActivator.PreApplicationStartMethod(typeof($rootnamespace$.App_Start.InstallCustomDataAnnotations), "Install")]

namespace $rootnamespace$.App_Start
    public class InstallCustomDataAnnotations
	public static void Install()
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredLocalizedAttribute), typeof(RequiredLocalizedAttributeAdapter));

So, this code needs a reference to system.web.mvc, which only makes sense in a MVC web app, but as my DTO.dll is also using DataAnnotations, it receives this code file and, of course, throws an error because of the dependency. I have the model in a separate assembly because I want to reuse it in other projects like Windows Forms or mobile, so I don't need that reference to mvc.

My question would be: Can I specify anyway in the nuspec file that "Content/App_Start/InstallCustomDataAnnotations.cs.pp" only applies to Web Application projects, and not to Code Library projects? May this be a new feature?

I hope I had explained myself :-)


Jun 2, 2011 at 8:47 AM

You can't do that today, you could make separate packages or write some powershell to make it work in each case the way you want it to.

Jun 3, 2011 at 6:54 PM
dfowler wrote:

You can't do that today, you could make separate packages or write some powershell to make it work in each case the way you want it to.

Thanks for answering, David.

I thought about that: making two separate packages -> DataAnnotations.Web (with web activator) and DataAnnotations.DLL (without it), but I discarded it because Nuget would install both on the Web project because of the dependencies:

DTO -> DataAnnotations.DLL

Web -> DTO (& DataAnnotations.DLL via dependency), DataAnnotations.Web (to use web activator)

How would you recommend using powershell? Can it detect the project type? Do you have any samples on how to remove content from a project with a powershell script?

Thanks again,