DotNetDevGuy

Monday, September 26, 2005

ASP.NET 2 Tip: How to Store Non-Deployed Filed in Web Projects

Scott Guthrie explains on his blog on how to deal with non-deployed files in your ASP.NET 2 Web Projects.

How to Store Non-Deployed Files in Web Projects

A final scenario that a few people have asked me about is how they might be able to store within Web projects, and under source control, files that can be used during development time (for example, design docs written in Microsoft Word or Microsoft Visio, or Adobe Photoshop .psd files for layered images), but that will be automatically excluded from any deployment or publishing steps.

A Technique to Make This Experience Much Better

Microsoft ASP.NET 2.0 supports the concept of "build providers." These are classes that implement the System.Web.Compilation.BuildProvider base class contract, and that can participate in build operations, both in Visual Studio 2005 at development-time, and in ASP.NET at runtime. Developers are free to build and implement their own BuildProviders, in order to add their own custom semantics to processing files within ASP.NET. For example, you could create an .orm file extension for files that contain XML, to declaratively represent an OR mapping database relationship. Your provider could then dynamically generate strongly typed classes that were included in the Web anytime one of these files was added to the project—and you would get both intellisense within Visual Studio 2005 at design-time, and also full runtime support.

One of the built-in build providers in ASP.NET 2.0 is called the IgnoreFileBuildProvider. Its semantics are such that it ignores whatever file extension is mapped to it, and that it also automatically prevents that file from being deployed during a publish Web operation or a compilation operation.

Developers can then use this feature to add extensions to the web.config file in their local Web projects, and effectively block any file-type they want from being deployed by Visual Studio. For example, if I wanted to prevent all .doc (Word), .psd (Photoshop), and .vsd (Visio) files from ever being deployed, I would add the following section to my web.config file.





type="System.Web.Compilation.IgnoreFileBuildProvider" />
type="System.Web.Compilation.IgnoreFileBuildProvider" />
type="System.Web.Compilation.IgnoreFileBuildProvider" />



I am then free to add and check in these file-types anywhere in my project if I want to do so—and they can be used/modified/accessed throughout the development lifecycle—but they will be excluded anytime a build is produced.