Click here to Skip to main content
12,406,592 members (49,618 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked

Extending ASP.NET 2.0 Part I: Creating Custom Build Providers

, 6 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Extend the ASP.NET runtime by creating custom build providers for custom project resources.

Sample Image - main.jpg


ASP.NET is a great managed environment that supports dynamic compilation for its registered resources. The aim of this article is to show how one can take advantage of dynamic compilation through the use of custom build providers.

The BuildProvider Class

The System.Web.Compilation.BuildProvider abstract class provides a set of methods and properties that allows ASP.NET to generate source code. To use this class, one must implement it and add an element under the system.web/compilation/buildProviders section under the web.config (or machine.config for your web server).

This list outlines how the ASP.NET build environment uses your instance of BuildProvider:

  • Build environment looks at the registered build providers within web.config.
  • An instance of your provider is created when the environment encounters a file extension within the App_Code special folder that matches the extension attribute of your configuration entry.
  • An AssemblyBuilder object is passed into your builder's GenerateCode method.
  • Your provider uses the AssemblyBuilder and other System.Web.Compilation classes to add source code for your file into the overall assembly.

To find out more about the BuildProvider class, please see the References section.

A ZipCode BuildProvider

This article uses a simple build provider that parses a file containing zip code information. The provider creates a state class for each line in the file and exposes the zip codes as properties. The custom file and the generated class look like this:

Contents of file All.zipcode:


Generated state class:

A custom zip class

As previously stated, the provider must be registered within web.config in order for the runtime to hook up your provider. The entry for the ZipCodeBuildProvider looks like:

    <add appliesTo="Code" 
        type="Lozanotek.Examples.ZipCodeBuildProvider, BuildProvider"/>

In order to generate source code that will be compiled into an assembly, you must make use of the classes under the System.Web.Compilation namespace to create an abstract syntax tree (AST) to give to the runtime through the AssemblyBuilder class. The CreateClass, CreateField and CreateProperty methods are used to create a custom state class by parsing a line in the file and creating their corresponding compilation units.

/// <span class="code-SummaryComment"><summary>


I hope this article has shown you, the developer, how to take advantage of the extensibility of the ASP.NET runtime. In my next Extending ASP.NET 2.0 article, I will demonstrate how to extend ASP.NET even further by creating custom expression builders.

Please feel free to make any comments or suggestions to this article by using the forums below.


During the writing of this article, I used the following resources:


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Javier Lozano
Web Developer
United States United States
Javier is a consultant specializing in ASP.NET, system architecture, and training. He's also a MCSD .NET and a co-founder of the Iowa DotNET Users Group. Recently, Javier was awarded the Visual Developer ASP.NET MVP award from Microsoft.

On his blog you can find posts on ASP.NET, architecture, design patterns, XML and just recently, Indigo. Javier enjoys giving back to the community by speaking at user groups, local/regional .NET events, being active in forums and by writing articles for Code Project.

In his spare time, Javier enjoys spending time with his wife, newborn son, two golden retrievers and writing about himself in third person.

Click here to read Javier's blog.

You may also be interested in...

Comments and Discussions

QuestionCan the build provider itself be dynamically compiled Pin
darin the developer8-Jan-06 5:06
memberdarin the developer8-Jan-06 5:06 
AnswerRe: Can the build provider itself be dynamically compiled Pin
Javier Lozano21-Jan-06 10:35
memberJavier Lozano21-Jan-06 10:35 
AnswerRe: Can the build provider itself be dynamically compiled Pin
Javier Lozano22-Jan-06 10:40
memberJavier Lozano22-Jan-06 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160730.1 | Last Updated 7 Nov 2005
Article Copyright 2005 by Javier Lozano
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid