<< SSIS Tries So Hard To Make You Hate It | Home | SQL Server 2005's EXCEPT Statement >>

Nant Task for Creating IIS Application Mappings

posted @ Monday, June 11, 2007 7:06 PM

Update -- Billy McCafferty has posted another way to add Extension Mappings to IIS via Nant.  Definitely effective, but I still think mine is easier and cleaner - keeps all that script mess out of your beautiful build script. 

We've been working on our continuous integration here, setting up CruiseControl.NET and cleaning up/improving our Nant build scripts.  Jean-Paul Boodhoo's series on Automating Your Builds with Nant has been an invaluable resource of great ideas and best practices (pretty much every post on his blog fits that description). 

Some of Jean-Paul's examples include pre-compiling and deployment of ASP.NET apps.  He uses the mkiisdir task in the Nant.Contrib library to create virtual directories via Nant.  One issue we were having was that several of our applications make use of additional application mappings in IIS for custom HTTPHandlers.  So the deployment task we had in our builds was only getting us part of the way there, and then we would have to add these mappings in manually to get our application to work completely.  Application mappings are also known as extension mappings or script mappings, but basically it amounts to mapping a file type (.jpg, .rails, .myextension) to a handler, usually the aspnet_isapi.dll if you are writing a custom HttpHandler.

At first I thought there must be a way to do this with the mkiisdir task, but it was not to be.  So, I decided to try to make my own Nant task to do so.  New to scripting IIS configuration, I turned to google.  After finding a few near-miss examples and discussions, I discovered the "ScriptMaps" property of the IIS Metabase.  Another useful tool was the ability to export your IIS settings to XML with IIS 6 - this basically serializes out the IIS Metabase settings for a website via the IIS management console.  You can then view them and get a feel for what you need to do to create a similar site. 

I started coding my task from scratch, when it dawned on me that Nant and NantContrib were open source projects, and why the heck wasn't I looking at the mkiisdir code for a good example of tinkering with the IIS Metabase!?  So, off I went to download the source, and noticed a very useful base task called "WebBase" in Nant.Contrib.Tasks.Web.  This task allowed me to use some of the same attributes as the mkiisdir task, and handled some of the communication with the IIS Metabase.  I added a few attributes to create the mapping string, and added the string to the "ScriptMaps" property collection.

The task call in your build script will look something like this:

<iisappmap vdirname="MLDB.Website"
extension=".jpg"
executable="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
verbs="GET,POST"
checkfileexists="false" />

The code can be found here (with comments describing usage) and the binary here.  To use in your builds, you will need to add to the appropriate area in your Nant bin directory - older versions just use <Nant>\bin\tasks I believe the latest uses <Nant>\bin\tasks\net

I tested this in IIS 6 and 7.  In IIS 7 it adds the mapping, but it adds it as an "Internal" mapping as opposed to the new "Local" mappings IIS 7 uses by default.  It caused no problems for me, but if you wanted the mapping to be added to your web.config, the way IIS 7's management tool does, this task will not do that for you.  Of course, if you put it in your web.config, you don't need this task!  Just deploy your web.config with your app :)

P.S.  I don't know much about open source etiquette, but seeing as this builds on top of NantContrib, I'd be happy to contribute it to that library if people find it useful.  I'm sure others will find ways to improve it, too!

kick it on DotNetKicks.com

Comments

  1. russ

    Posted on: 7/24/2007 5:32 PM

    # re: Nant Task for Creating IIS Application Mappings

    Just wanted to say thanks for the extremely useful nant task.

  2. Brian Donahue

    Posted on: 7/24/2007 10:27 PM

    # re: Nant Task for Creating IIS Application Mappings

    No problem at all, Russ. It was easier than I thought, thanks to the existing IIS tasks in Nant.Contrib. I hope many people find it useful.

  3. Sethi

    Posted on: 8/8/2007 12:47 PM

    # re: Nant Task for Creating IIS Application Mappings

    I've been trying to get this task done by vbscript.
    Any idea how can I add a new ScriptMap for a website through some WMI command.
    That'll be a great help.
    Cheers.

  4. Brian Donahue

    Posted on: 8/9/2007 4:36 PM

    # re: Nant Task for Creating IIS Application Mappings

    Sethi,

    I don't know off-hand, but if you follow some of the microsoft links in my post, it shows you how script mappings can be added to the IIS MetaBase. You'd just need to access the MetaBase via VBScript and add the appropriate properties.

  5. bob macneal

    Posted on: 8/17/2007 4:30 PM

    # re: Nant Task for Creating IIS Application Mappings

    I spent all afternoon figuring out how to get a handle to the scriptmaps property and try to add an entry to it's collection via Nant...ugh. This is just the ticket! Thanks for posting it.

  6. Brian Donahue

    Posted on: 8/28/2007 11:48 AM

    # re: Nant Task for Creating IIS Application Mappings

    Bob - I'm just glad that people are finding this, and finding it useful. Thanks!

  7. Eugen Anghel

    Posted on: 10/23/2007 5:10 AM

    # re: Nant Task for Creating IIS Application Mappings

    Great work!
    One small problem though, the expression @"\.[a-zA-Z0-9]+" is too restrictive. To do url rewriting without an extension you need to map ".*" to aspnet, so I guess the regex should be @"\.[a-zA-Z0-9]+|\.*".

  8. Jeroen van Menen

    Posted on: 12/17/2007 9:25 AM

    # re: Nant Task for Creating IIS Application Mappings

    Thanks for the Nant task. It does the job nicely!
    Jeroen

Your comment:



 (will not be displayed)


  Please add 2 and 6 and type the answer here: