Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Overriding file contents in an ASP.NET 2.0 application using Virtual Path Providers

After reading Victor's article about Virtual Path Providers I thought it was a very cool technology and that it was possible to do very interesting things with it.

One of the things I wanted to try was to be able to kind of override a single file in an asp.net application in a specific installation of the web app.

If you are using a code generation tool for the ASP.NET presentation layer, it's very possible that you will want to radically change some of the generated files, and leave the rest as they are. There are several solutions to this problem, but using a VPP looks like a very good one. Instead of modifying the file and finding a way to tell the code-generator to not to overwrite it, you can create a folder called 'Branchs', and put the modified file there. You can put just the .aspx/.ascx, or also the .cs, or the .css, or whatever you want.

The other scenario that is more appealing to people that does not use code generation for their ASP.NET layer, is application customization. Suppose you want to install a single application for several customers, and each customer requires minor tweaks in the UI. You can build complex plumbing to support that, and based on metadata customize the pages in runtime, or you can just put the modified files in a 'Customer1' folder, and make the VPP return those ones.  Of course, it's not the best from a maintenance point of view. If you need to make a change in a page for two customers, you'll need to do it twice, but this problem is not easy to solve anyway, so I'm not sure how bad it is.

You could also have a hierarchy, like a folder for each country with country-specific customizations, and below that one, the customer specific ones. The VPP will first look in the customer specific ones, then in the country-specific ones and then in the main web directory.

Russ Garner, one of my favorite DeKlarit users actually built it. Check it here.

No Comments