BusinessRx Community

Dedicated to the advancement of software, technology and the people who devote their lives to it.

Welcome to BusinessRx Community Sign in | Join | Help
in Search

BusinessRx Reading List

These blog entries are written by industry experts and leaders. We consider this content to be a good read for any software developer or web technologist.

Browse by Tags

All Tags » Tips and Tricks » ASP.NET » Visual Studio   (RSS)

  • Tip/Trick: Hard Drive Speed and Visual Studio Performance

    People often ask me at conferences for PC hardware recommendations.  Specifically - "what type of machine do you recommend I get for doing development with Visual Studio?" and/or "your laptop seems really fast, what type is it?"

    Some of my recommendations on this topic are fairly standard and obvious: Ideally you want to get a duel core or better CPU.  I also always recommend getting at least 2GB or more of RAM. 

    The recommendation I make that often seems to take people a little by surprise is to make sure you always get the fastest possible hard-drive when buying a new machine - and where necessary trade off purchasing additional CPU processor speed in favor of investing in a faster disk instead.

    Why does hard drive speed matter?

    Multi-core CPUs on machines have gotten fast enough over the past few years that in most common application scenarios you don't usually end up blocking on available processor capacity in your machine.

    What you are much more likely to block on is the Seek and I/O speed capacity with which your computer accesses your hard drive.  If you are using an application that needs to read/write a lot of files, it is not atypical for your CPU processor utilization to be really low - since the application might be spending most of its time just waiting for the disk operations to complete.

    When you are doing development with Visual Studio you end up reading/writing a lot of files, and spend a large amount of time doing disk I/O activity.  Large projects and solutions might have hundreds (or thousands) of source files (including images, css, pages, user controls, etc).  When you open a project Visual Studio needs to read and parse all source files in it so as to provide intellisense.  When you are enlisted in source control and check out a file you are updating files and timestamps on disk.  When you do a compilation of a solution, Visual Studio will check for updated assemblies from multiple disk path locations, write out multiple new assemblies to disk when the compilation is done, as well as persist .pdb debugger symbol files on disk with them (all as separate file save operations).  When you attach a debugger to a process (the default behavior when you press F5 to run an application), Visual Studio then needs to search and load the debugger symbols of all assemblies and DLLs for the application so as to setup breakpoints.

    If you have a slow hard-drive, Visual Studio will end up being blocked as it waits for it to complete these read/write operations - which can really slow down your overall development experience. 

    Some laptop hard drive recommendations

    The default hard drive speed for most PC laptops is typically 5400rpm - which is a pretty slow drive.  If you are getting a new laptop and plan to use Visual Studio on it, I highly recommend making sure you get a 7200rpm drive instead.  You will realize a significant performance benefit by doing this.

    You might assume if you are buying a top of the line laptop that "of course" it won't have a slow drive.  Don't assume this!  Check out the default configuration of this high-end Thinkpad (which is the brand I use), or the default configuration of the high-end Dell XPS laptop (whose list price starts at $3,343), or the default configuration of the 17" Apple MacBook Pro laptop ($2,799).  Notice that by default all three of these premium laptop lines have 5400rpm drives.

    For an additional $55 (Lenovo), $93 (Dell) or $200 (Apple) you can upgrade the hard drive to be a 7200rpm disk instead.  Putting this small amount of extra money in the faster disk can really make a big performance difference and is an absolutely worthwhile investment.

    Some desktop machine hard drive recommendations

    Two hard drive performance considerations to consider with desktop configurations:

    1) Consider getting a 10,000rpm hard drive.  These are lightening fast and can make a big difference over the default 7,200rpm drives that typically come with desktops (Jeff Atwood has a good blog post recommending one of these). 

    2) Consider buying a second physical hard drive and setup your operating system and OS virtual memory swap file to use one of the physical drives, and then store all your data (images, documents and Visual Studio projects) on the second physical drive.  The benefit of an approach like this is that your read/write data operations won't be competing for disk I/O activity with the operating system updating the virtual memory file.

    Other Visual Studio Performance Recommendations

    While on the topic of improving Visual Studio performance, you might want to also check out the blog posts and articles below for some good recommendations: 

    One other performance gotcha I've heard about recently is an issue that a few people have reported running into with the Google Toolbar add-in.  For some reason this can sometimes cause long delays when attaching the Visual Studio debugger to the browser.  If you are seeing long delays with your web application loading, and have the Google Toolbar (or other toolbars) installed, you might want to try uninstalling them to see if that is the cause of the issue.

    Hope this helps,

    Scott

    P.S. For more of my tip/trick posts, please check out my ASP.NET Tips, Tricks and Tutorials page.

  • Tip/Trick: Building a ToJSON() Extension Method using .NET 3.5

    Earlier this year I blogged about a new language extensibility feature of C# and VB called "Extension Methods". 

    Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.  In doing so they enable a variety of useful scenarios (including LINQ).  They also provide a really convenient way to add a dash of "syntactic sugar" into your code.

    Over the last few months I've been making a list of cool extension methods that I plan to sit down and implement when I get some free time (not sure when that is... but at least I can still have fun coming up with the ideas!)  Two of the scenarios I added to my extension method list were easy methods to automate generating JSON (JavaScript Object Notation) or XML serialization strings for any .NET object. 

    Simple Scenario: The ToJSON() extension method

    Assume I had a Person object defined like below (note: I'm using the new automatic properties feature to implement it):

    I'd like to then be able to initialize a collection of Person objects and programmatically retrieve a JSON string representation of them by just calling a ToJSON() extension method on it like below:

    This would work just like the built-in ToString() method on the Object class in .NET today - except that it would generate a JSON-format representation of the collection that I could use for AJAX scenarios on the client:

    Note: Clicking on the hour-glass in the debugger above allows us to bring up the Text Visualizer in VS to see a clean version of the JSON serialization:

    This string format could then be used within JavaScript on the client to instantiate an appropriate JavaScript object that represents my collection (note: ASP.NET AJAX has a built-in JavaScript library to support this).

    Implementing the ToJSON Extension Method

    Implementing a basic ToJSON() extension method is pretty simple.  All I needed to-do was use the JavaScriptSerializer class in the System.Web.Script.Serialization namespace, and define two extension methods like below. One of the methods serializes an object graph any levels deep, the other is an overloaded version that allows you to optionally constrain how deep it recurses (for example: ToJSON(2) would serialize only 2 levels deep in the object graph).

    Note that the ToJSON() extension methods above are defined for type "Object" - which means they can be used with all objects in .NET (not just collections).  This means that in addition to calling .ToJSON() on collections like I did above, I could also have called ToJSON() on individual Person objects, as well as any other .NET datatype.

    To use the extension method, all I need to-do is add a using statement at the top of my program that references the namespace it was defined within:

    VS 2008 then takes care of providing intellisense and compile time support for it to all objects:

    Note: In addition to the JavaScriptSerializer class, .NET 3.5 also now includes a new System.Runtime.Serialization.DataContractJsonSerializer class that you can use for JSON serialization/deserialization.

    Summary

    Hopefully the above sample provides a simple example of how you can easily encapsulate useful functionality into extension methods.  Overtime I expect that we'll start to see some nice utility libraries come out that provide helpful extension methods like above. 

    I'd be curious to see suggestions for other common scenarios you think should be packaged up into re-usable extension methods (feel free to use the comments of this post to suggest them).  We can then figure out how to get a good CodePlex project created that bundles up some of them together into one library to easily use.

    Hope this helps,

    Scott

    P.S. Please check out my Tips/Tricks and Tutorials page to find other useful ASP.NET and .NET posts I've done.

  • Tip/Trick: Automating Dev, QA, Staging, and Production Web.Config Settings with VS 2005

    One of the questions I get asked fairly regularly is: "how can I can easily change different configuration settings in my web.config file based on whether my application is in a dev, qa, staging or production mode?"  The most common scenario for this is one where an application uses different database connection-strings for testing and production purposes.

    It turns out you can easily automate this configuration process within the Visual Studio build environment (and do so in a way that works both within the IDE, as well as with command-line/automated builds).  Below are the high-level steps you take to do this.  They work with both VS 2005 and VS 2008.

    1. Use ASP.NET Web Application Projects (which have MSBuild based project files)
    2. Open the VS Configuration Manager and create new "Dev", "QA", "Staging" build configurations for your project and solution
    3. Add new "web.config.dev", "web.config.qa", and "web.config.staging" files in your project and customize them to contain the app's mode specific configuration settings
    4. Add a new "pre-build event" command to your project file that can automatically copy over the web.config file in your project with the appropriate mode specific version each time you build the project (for example: if your solution was in the "Dev" configuration, it would copy the web.config.dev settings to the main web.config file).

    Once you follow these steps, you can then just pick the mode your solution is in using the configuration drop-down in the VS standard toolbar:

    The next time you build/run after changing the configuration mode, VS will automatically modify your application's web.config file to pick up and use the web.config settings specific to that build configuration (so if you select QA it will use the QA settings, if you select Deploy it will use the Deploy settings).

    The benefit with this approach is that it works well in a source control environment (everyone can sync and build locally without having to make any manual changes on their local machines).  It also works on a build server - including with scenarios where you are doing automated command-line solution builds.

    To learn more about the exact steps to set this up, please read the Managing Multiple Configuration File Environments with Pre-Build Events post that Scott Hanselman published earlier tonight.  Also check out my ASP.NET Tips, Tricks, and Gotchas page for other ASP.NET Tips/Tricks recommendations.

    Hope this helps,

    Scott

  • Public Hotfix Patch Available for VS 2005 F5 Debugging Performance Issue with ASP.NET

    Over the last year we've seen several people report having performance problems when using F5 to run/debug ASP.NET 2.0 applications using Visual Studio 2005.  The problem in theses cases typically wasn't with building the project, but rather that it sometimes took the debugger a really long time to hit the first breakpoint once it attached to the application (45+ seconds and longer sometimes).

    We recently posted a public hotfix patch that you can download and apply to fix this issue.  You can download the hotfix immediately here.  You can read more about it in this KB article here.

    Background on the Issue

    The particular bug fixed in this hotfix was surprisingly difficult to track down.  The slowdown issue occurred when breakpoints were set in multiple source files in the same project that had the same short file names (for example: two default.aspx.cs files) and which were organized in a certain directory structure pattern on disk.  The patch fixes this issue and should prevent the debugger from pausing when trying to load the web project's assembly symbols in this case.

    For Technical Support with this Patch

    If you have any problems installing the patch or find that you still see issues after you install it, you'll want to contact Microsoft product support for assistance and they'll help debug it further.  Calls to Microsoft Product Support are free if they are related to a product bug (either a QFE HotFix request or a product bug you are running into).  You can find details on how to contact Microsoft product support on this page (it allows you to lookup the local phone number to use by country).

    Other Hotfix Patches

    I've posted in the past about other ASP.NET and VS related hotfix patches that can be publicly downloaded.  Here are a few pointers to these posts:

    More Performance and Tip/Tricks/Tutorial Posts

    Make sure to check out my Tip/Trick: Optimizing ASP.NET 2.0 Web Project Build Performance with VS 2005 post if you have build performance issues with VS 2005.

    Please visit and bookmark my ASP.NET 2.0 Tips, Tricks, Recipes, and Gotchas page for a good listing of some of my previous ASP.NET and Visual Studio posts and tutorials.

    Hope this helps,

    Scott

  • July 4th Links: ASP.NET, ASP.NET AJAX, Visual Studio, Silverlight and IIS7

    I've fallen behind on my weekly link-listing series - apologies for the delay.

    ASP.NET

    • ASP.NET RSSToolkit 2.0 Released: One of the cool projects for ASP.NET 2.0 that was released last year was this free RSS Toolkit - which makes consuming and exposing RSS feeds in ASP.NET super easy (you can even databind any ASP.NET control against them).  The team working on the CodePlex project has recently released V2 of the RSSToolkit.  You can learn all about it and download it here.

    • Building a Custom Database Driven Site Map Provider: Scott Mitchell has written a great article on how to implement your own site map provider for ASP.NET that is populated from a database (instead of statically from an XML file).  You can learn more about the ASP.NET 2.0 SiteMap system from this older blog post of mine here.

    • .NET DateTime and Number Format String Cheat Sheet: If you are like me, you might have trouble remembering all of the standard format strings you can pass to the String.Format() method and/or the Eval() databinding method in ASP.NET to generate the appropriate string output from a DateTime or Numeric datatype.  This PDF cheatsheet is a useful one to download and save to quickly look these format strings up.  John has some other really useful .NET PDF cheatsheets he has also created that you might like to download here.

    • Profile Support for ASP.NET Web Application Projects: VS 2005 Web Application Projects can't directly access the strongly-typed ASP.NET "Profile" object that web site projects support.  This VS add-in supports the ability to generate a strongly typed profile class to accomplish this.  You can read this great series of posts to learn more about how to use the ASP.NET 2.0 Profile system.  I have it on my list of tips/tricks posts to-do to cover using this VS add-on as well.

    • ASP.NET Photo Handler: Bertrand has posted a cool photo album HttpHandler for ASP.NET that allows you to easily drop images into a web directory and automatically generate a nice photo album of them (complete with EXIF information, stack sorting icons, etc).  Might be very useful for people enjoying holidays this summer.  Download the code here.

    • BlogEngine.NET: This is a new open source blog engine for ASP.NET that Mads Kristensen has helped start up, and which I've heard a lot of good things about.  You can read about its features here, and download it here.

    ASP.NET AJAX

    • ScriptDoc 1.0 Available: Bertrand Le Roy has published a cool ScriptDoc utility that extracts documentation from JavaScript files and packages it into XML that can be consumed by documentation building tools.  A very useful tool as you start to build up your own JavaScript libraries.

    Visual Studio

    • GhostDoc 2.1.1 Released: GhostDoc is a free add-in for Visual Studio 2005 (and now 2008) that automatically generates default XML documentation comments for code you write in C# or VB.  It can automatically re-use existing documentation inherited from base classes or implemented interfaces, or generate initial documentation by deducing comments from the name and type of the member signature. You can learn more about it and download it for free here.

    Silverlight

    • Silverlight Tutorials: Michael Schwarz has a great blog where he writes regularly about Silverlight.  This tutorials link points to a bunch of great Silverlight content.

    IIS 7

    • IIS 7.0 is now running all of Microsoft.com: One of the things we push at Microsoft is to "dogfood" our products on our high volume sites when they enter the beta cycle.  As of a few weeks ago, all of the web servers running www.microsoft.com are now running on IIS7 and Windows 2008 Server Beta3.  These servers host 500+ virtual roots and 350 ASP.NET applications, and handle 300,000 concurrent connections.  IIS7 is going to be an awesome release.

    • IIS 7.0 on Server Core: Bill Staples blogs about some of the new IIS7 enhancements that appear with the June CTP of Windows 2008 Server.  One of the big features that is now supported is the ability to install IIS7 on "server core" - which is a low footprint installation of Windows 2008 Server that lays down just the minimal footprint needed to boot (meaning no GUI shell).  This lowers the resources required on servers, and even more importantly means that servers don't need to be updated if a patch is released for a component not installed on the server (which lowers the downtime of servers).  ASP.NET and the .NET Framework aren't supported yet in server core configurations - but will be in the future.

    Hope this helps,

    Scott

    P.S. I'm out on vacation this week, so please excuse delays on email and comment feedback.

  • Tip/Trick: Creating Packaged ASP.NET Setup Programs with VS 2005

    Scenario

    You have built an ASP.NET Web Application using Visual Studio 2005, and want to enable customers to automatically install and deploy it on servers via an easy setup program.

    Specifically, you want to create a standard Windows setup program that will create and configure the application on IIS, copy all of the application’s files to the appropriate location on the server, and ensure that ASP.NET 2.0 is correctly mapped to run the application. You also want the setup program to prompt the customer for the database location that the new application should use, and have the setup program automatically update the web.config file with the database connectionstring settings the customer provided.

    One solution to consider using is the built-in "Web Setup Project" support that is built-in to Visual Studio 2005.  Web Setup Projects can be used to pipe the compilation outputs from VS 2005 Web Application Projects as well as Web Site Projects (when used with VS 2005 Web Deployment Projects), to create encapsulated Windows setup programs. The below walkthrough demonstrates step-by-step how to create and use one.

    1) Create a VS 2005 Web Application Project

    To begin with, we’ll start with an empty instance of Visual Studio and create a new VS 2005 Web Application project (select File->New Project->ASP.NET Web Application).  For the purposes of this simple sample we’ll have two pages in the project:

    We’ll add a label to the Default.aspx page and a Page_Load event handler in the code-behind to output the current timestamp on each request. When I press F5 to build and run the application, the project will compile and run as I’d expect (and by default use the built-in VS Web Server):

    2) Add a VS 2005 Web Setup Project to the Solution

    Now that we have a simple ASP.NET application built, we’ll want to add a VS 2005 Web Setup Project to the solution. Choose the File->Add->New Project menu item to add one into your solution:

    Note that the “Web Setup Project” type shows up under the “Other Project Types->Setup and Deployment” node in the New Project dialog above. Name it whatever you want and hit ok. It will then show up in your solution explorer as a separate project.

    Our next step will be to configure the web setup project to take the compiled assemblies (\bin directory contents) + content markup (.aspx, .config, etc files) from our Web Application Project and use them as inputs within our setup project. To-do this, right-click on the web setup project node in the solution explorer and choose the “Add->Project Output” context menu item:

    A dialog will then appear allowing us to select which project in the solution, and which of its project contents, we want to add to the setup package:

    For ASP.NET Web Application Projects it is really important that we select both the “Primary Output” (which are the compiled assemblies for the \bin directory) as well as the “Content Files” (which are the .aspx markup files) within this dialog.

    By default, the web setup project will copy both of these items into the root of the target Web Application Folder that the setup project will create. You can see that it is configured this way by opening up the “File System” view within the web setup project (right click on the web setup project root and choose View->File System):

    This actually isn’t what we want to have happen though – since we really want the assemblies (indicated by the Primary Output node) to be copied into the application’s \bin directory instead (otherwise ASP.NET won’t be able to find them at runtime). To fix this, drag/drop the “Primary Output from MyApplication” node into the \bin directory. Once this is done you should be able to click on the “Web Application Folder” node on the left-hand side and see this:

    And then click on the “bin” folder sub-node and see this:

    We now have a basic web setup project created and configured for our ASP.NET Web Application. Next step is to build and run it.

    3) Build and Run the VS 2005 Web Setup Project to the Solution

    To build the web-setup project we can right-click on the web setup project node within the solution explorer and choose the “Build” option:

    If you open the output window within VS (View->Output menu item), you will see the results of this build operation:

    Our “MyApplicationSetup” project created a new MyApplicationSetup.msi Windows installer file and compressed and packaged the contents of our ASP.NET Web Application (note: in the web setup project properties dialog you can choose whether the compression algorithm used is optimized for size or speed).

    Very Important: Because setup projects take awhile to build, they are by default marked not to build as part of the solution.  What this means is that you need to right-click on them and explicitly do a build in order for them to be recompiled.  Be careful to-do this when you make and test changes - otherwise you'll be running the previously compiled version and not the one with your latest code!

    To test it, we can right-click on the web setup project within the solution explorer and choose the “Install” option to install it (or alternatively launch it outside of VS by running it):

    This will launch a standard Windows installer and walk the user through installing the application on IIS:

    VS 2005’s web setup projects allow you to pick which site to install the application on if multiple sites are configured on IIS (this wasn’t supported with the VS 2003 version). You can optionally specify an application virtual-directory path to use (for example: http://www.myserver.com/myapppath), or you can leave this value blank to install it as the root application on the site (for example: http://www.myserver.com/).

    Once the installer completes, the application will have been copied to disk and registered with IIS. We can now run the application using the HTTP path we provided during installation like so:

    Once installed the application will also show up in the standard “Add or Remove Programs” utility within the Windows Control Panel:

    You can remove the application either by running uninstall from the control panel utility, or (at development time) by right-clicking on the web setup project node within the VS Solution Explorer and selecting the “Uninstall” menu item. This will cause all installed files to be removed from disk.

    4) Update the Wizard UI of the Web Setup Project

    By default the Windows installer created by a web setup project has some default instruction strings and banner images for the setup:

    You can change this and customize the screens by right-clicking on the web setup project node in the VS solution explorer and selecting the "View->User Interface" context menu item):

    This will then bring up a screen that shows the list of screens to be displayed during setup:

    Unfortunately there isn't a forms-designer that you can use to override the screens above.  However, you can select a screen, and then go to the property grid to customize its text and change the graphics used within the screen:

    You can also create new screens and add them into the setup wizard.  Later in this tutorial we'll use this feature to create a custom screen to collect database connection-string information and use it to automate configuring our web.config file to point at the appropriate database.

    5) Adding Custom Actions to the VS 2005 Web Setup Project

    Web Setup Projects contain built-in support for configuring and performing common setup actions. These include editors for adding/changing registry entries (choose View->Register to configure), changing file-type associations (View->File Types), and for validating prerequisite components are already installed (it automatically checks that the .NET Framework 2.0 redist is installed). Setup Projects also allow you to configure a number of common IIS settings declaratively (click on the “Web Application Folder” in the File System view and then look at the property grid to see these):

    But for non-trivial setups you are likely to want to be able to execute your own custom code during setup to customize things. The good news is that web setup projects support this with something called “Custom Actions” – which is code you write that can execute during both install and uninstall operations.

    To add a custom action you first want to add a new class library project to your solution (File->Add->New Project->Class Library). 

    You then want to add assembly references in this newly created Class Library to the System.Configuration.Install.dll, System.Configuration.dll, System.Diagnostics.dll, and System.Web.dll assemblies. You’ll then want to create a new class for your custom action and have it derive from the “System.Configuration.Install.Installer” base class like so:

    using System;
    using 
    System.Configuration.Install;
    using 
    System.ComponentModel;

    namespace 
    MyCustomAction
    {
        [RunInstaller(
    true)]
        
    public class ScottSetupAction : Installer
        {
            
    public override void Install(System.Collections.IDictionary stateSaver)
            {
                
    base.Install(stateSaver);

                
    // Todo: Write Your Custom Install Logic Here 
            
    }
        }
    }

    Notice the custom “RunInstaller(true)” attribute that must be set on the class name. This is important and required (and easy to forget!). You’ll need to add a using statement to the System.ComponentModel namespace to avoid fully qualifying this.

    Next we’ll need to make sure this Custom Action assembly gets added to our web setup project. To-do this, right-click on the Web Setup Project root node in the solution explorer and select the View->File System menu item to bring up the file-system editor. Right-click on the “bin” sub-folder and choose “Add->Project Output” like we did earlier to get the custom action assembly added to the web setup project:

    In this case we’ll want to select the Custom Action Class Library project instead of our web application one. Pick it from the project drop-down at the top of the dialog and then select the “Primary Output” option as the piece we want to add to the web-setup project (this will cause the Custom Action assembly to get added):

    Lastly, we’ll configure the web-setup project to call our custom action assembly during the install phase of setup. To do this we’ll right-click on the web setup project root node in the solution explorer and choose the “View->Custom Actions” menu item. This will then bring up the Custom Actions Editor. Right-click on the “Install” node and choose “Add Custom Action”:

    Drill into the Web Application Folder and Bin directory and choose the output from our Custom Action we imported:

    The Setup Project will then automatically detect the custom action because of the “RunInstaller” attribute:

    Our custom action class and Install method will now run anytime we run the installation setup program.

    6) Useful Custom Action Example: ASP.NET Script Mapping Checker

    The previous section showed how to create and configure an empty custom action class and install method. Let’s now do something useful with it. Specifically, let’s add code to verify that the right version of ASP.NET is correctly mapped for the application we are creating.

    Because ASP.NET V1.1 and V2.0 can run side-by-side with each other on the same machine, it is possible to have different parts of a web server configured to run using different versions of ASP.NET. By default, the versions inherit hierarchically – meaning if the root application on a site is configured to still run using ASP.NET V1.1, a newly created application underneath the site root will by default run using V1.1 as well. What we’ll do in the steps below is write some code to ensure that our new application always runs using ASP.NET 2.0.

    To begin with, we’ll select our custom action within the Custom Action explorer (just like in the previous screenshot above - using the View->Custom Action context menu item). We’ll then go to the property grid and specify a few parameters to pass to our custom action to use:

    Specifically, we’ll pass in the target directory that the application is being installed in, the IIS site map path, and the IIS virtual directory name that the user specified in the setup wizard. This string of values looks like this:

    /targetdir="[TARGETDIR]\" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"

    We’ll then update our custom action to access these values and do something with them like so:

    using System;
    using 
    System.Configuration;
    using 
    System.Configuration.Install;
    using 
    System.ComponentModel;
    using 
    System.Diagnostics;
    using 
    System.IO;

    namespace 
    MyCustomAction
    {
        [RunInstaller(
    true)]
        
    public class ScottSetupAction : Installer
        {

            public override void 
    Install(System.Collections.IDictionary stateSaver)
            {
                
    base.Install(stateSaver);

                
    // Retrieve configuration settings
                
    string targetSite Context.Parameters["targetsite"];
                string 
    targetVDir Context.Parameters["targetvdir"];
                string 
    targetDirectory Context.Parameters["targetdir"];

                if 
    (targetSite == null)
                    
    throw new InstallException("IIS Site Name Not Specified!");

                if 
    (targetSite.StartsWith("/LM/"))
                    targetSite 
    targetSite.Substring(4);

                
    RegisterScriptMaps(targetSite, targetVDir);
            
    }

            
    void RegisterScriptMaps(string targetSite, string targetVDir)
            {
                
    // Calculate Windows path
                
    string sysRoot System.Environment.GetEnvironmentVariable("SystemRoot");

                
    // Launch aspnet_regiis.exe utility to configure mappings
                
    ProcessStartInfo info = new ProcessStartInfo();
                
    info.FileName Path.Combine(sysRoot, @"Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe");
                
    info.Arguments = string.Format("-s {0}/ROOT/{1}", targetSite, targetVDir);
                
    info.CreateNoWindow = true;
                
    info.UseShellExecute = false;

                
    Process.Start(info);
            
    }
        }
    }

    The above code launches the aspnet_regiis.exe utility that ships with ASP.NET within the \Windows\Microsoft.net\framework\v2.0.5.0727\ directory, and passes in the path location information for the site that the web setup installer previously created, along with the “-s” flag – which indicates that the IIS script-maps for that application should be updated to specifically use the ASP.NET 2.0 version, and not inherit the version number from any parent applications.

    A special thanks to John for figuring this out in his blog post here.

    Note: If you are using IIS6 or IIS7, you'll probably want to also add some logic into the custom action to ensure that the application pool that the application is being hosted in is also mapped to use ASP.NET 2.0.  Either that or you'll want to tell the admin to manually check the application pool settings after the setup is complete.

    7) Useful Custom Action Example: Configuring Database Connection String

    For our next custom action example, let’s add some UI to the setup that allows a user to configure the connection string details of a database the application should use.

    Right click on the web setup project and open up the user interface screens again:

    Right click on the "Install" node on the user interface screens page and chose to add a new dialog to the install wizard:

    Chose one of the TextBox screens to use for gathering connection string details from the user:

    Right-click on the TextBox screen node and move it up to be earlier in the wizard (right after we pick the IIS site and application name to use):

    Then click on the TextBox screen and access its property window.  Via the property window you can change the text displayed on the screen, as well as control how many textboxes are visible:

    Note in the above property window how I've set the Edit2, Edit3 and Edit4 text boxes to not be visible.  Now when we build and run the setup package we'll see this dialog in our wizard steps:

    Now that we have UI to capture the connection-string value entered by a user in the wizard, we want to make sure it is passed to our custom action class.  You can do this by right-clicking on the web setup project node and by then choosing the "View->Custom Actions" context menu and then opening the property page window of our custom action:

    We'll want to update the CustomActionData property value and pass in the connection-string of the database to use (we'll pass in the value from the EDITA1 textbox in the user interface screen):

    /targetdir="[TARGETDIR]\" /db="[EDITA1]" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"

    We can then update our custom action class to retrieve and use this connectionstring value to update the web.config file of the new application to contain the value the user installing the application entered. Below is a method that opens the web.config file for our new application and programmatically updates it with the user entered connection string:

    void ConfigureDatabase(string targetSite, string targetVDir, string connectionString)
    {
        
    // Retrieve "Friendly Site Name" from IIS for TargetSite
        
    DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/" + targetSite);
        string 
    friendlySiteName entry.Properties["ServerComment"].Value.ToString();

        
    // Open Application's Web.Config
        
    Configuration config WebConfigurationManager.OpenWebConfiguration("/" + targetVDir, friendlySiteName);

        
    // Add new connection string setting for web.config
        
    ConnectionStringSettings appDatabase = new ConnectionStringSettings();
        
    appDatabase.Name DATABASE_CONNECTION_KEY;
        
    appDatabase.ConnectionString connectionString;

        
    config.ConnectionStrings.ConnectionStrings.Clear();
        
    config.ConnectionStrings.ConnectionStrings.Add(appDatabase);

        
    // Persist web.config settings
        
    config.Save();
    }

    And now after we run the setup program our newly installed ASP.NET application's web.config file will have been updated to point to the right database.

    To learn more about how the ASP.NET configuration APIs can be used to make changes to web.config files, please check out the management API section in the ASP.NET 2.0 Quickstart tutorials.  Chris Crowe also has some useful samples that demonstrate how to use the System.DirectoryServices APIs to query IIS settings (I needed them to figure out how to lookup the "friendly name" of the site from IIS to open up the web.config file).

    You might also want to check out this MSDN documentation sample that demonstrates how to programmatically create a new database (complete with schema and data) with a custom action.  You could combine the approach in the MSDN article with the configuration one I used above to completely automate database deployment as part of your setup.

    Summary

    Hopefully the above tutorial helps demonstrate how to get started with using the built-in web setup project support within Visual Studio.  Click here to download a complete version of the sample I built above.

    Web setup projects aren't perfect for all scenarios, and I'd primarily recommend them only for cases where you want a packaged GUI setup program (for example: to give to an external customer or to make available as a download on a web-site).  If you are instead working on maintaining/managing a site that you have direct access to, I'd probably instead recommend using the "Publish Application" feature available with VS 2005 Web Application Projects (for simple updates), or recommend authoring a PowerShell script to automate updates to the remote server.  For an example of a really advanced Powershell script that www.pageflakes.com uses to update their site, check out Omar's article here.

    One downside with the VS 2005 Web Setup Project support is that you can only build web setup projects from within the IDE - which means you can't completely automate the creation of .MSIs as part of an automated MSBuild process.  If this is a showstopper for you, you should consider looking at the WIX setup framework - which does support this scenario. 

    You can find a good set of WIX Tutorials here.  If someone wants to publish a blog post that demonstrates how to perform the scenarios I outlined in the blog post above using WIX, let me know and I will definitely link to it (and send you a few .NET books to say thanks!).

    Hope this helps,

    Scott

    P.S. Please check out my ASP.NET Tips, Tricks and Tutorials page for more cool ASP.NET samples and tips/tricks.

  • ASP.NET 2.0 Tips and Tricks and ASP.NET, IIS7 and ASP.NET AJAX End to End Talks

    I've finished my whirlwind speaking tour of Europe (Belgium, UK and Netherlands in less than one week), and was fortunate to have had the chance to present to several hundred people along the way.  Many thanks to everyone who came out to attend my talks!

    Here are the first two slide decks + samples from the talks I gave:

    ASP.NET Tips and Tricks: 

    This talk covered ASP.NET UI, AJAX, Caching and Deployment Tips and Tricks, and Visual Studio 2005 tips/tricks.  The samples include updated versions of ASP.NET AJAX that all work with ASP.NET AJAX 1.0.  You can download the slides+samples here.

    You can learn more about the Visual Studio build performance optimization suggestions by reading this past post of mine on improving build performance.  You can also find several dozen more ASP.NET and Visual Studio Tips/Tricks of mine on my Tips and Tricks summary page.

    Building an End-to-end Web Application from scratch using ASP.NET 2.0, ASP.NET AJAX, IIS7 and Visual Studio:

    This talk walked through building a rich data-driven application from scratch using ASP.NET 2.0, ASP.NET AJAX 1.0, IIS7 and Visual Studio (all of which are now officially shipping products).  You can download the deck + completed sample here.

    This talk demonstrates the following features: IIS7 distributed configuration, Master Pages, Building a DAL using DataSets, Building Data Driven Edit Pages, Building Catalogue Browsing Pages, Site Navigation, Url Rewriting, Site Navigation, CSS Control Adapters, ASP.NET AJAX, ASP.NET AJAX Control Toolkit, Membership/Roles, IIS7 Admin UI, SQL Cache Invalidation, and WebPart portal support.

    Hope this helps,

    Scott

    P.S. I'll be posting more slides+samples from my other talks in the days ahead (including my Orcas and WPF/E talks).

  • Recipe: Deploying a SQL Database to a Remote Hosting Environment (Part 1)

    Scenario:

    You finish building a great ASP.NET application, have everything tested and working right on your local system, are taking full advantage of the new ASP.NET 2.0 Membership, Role and Profile features, and are ready to publish it to a remote hosting environment and share it with the world. 

    Copying the .aspx files and compiled assemblies to the remote system is pretty easy (just FTP or copy them up).  The challenge that confronts a lot of developers, though, is how to setup and recreate the database contents - both schema and data - on the remote hosted site.  Unfortunately there hasn't historically been a super-easy way to accomplish this.

    The good news is that this week the SQL Server team published the release candidate of a new SQL Server Hosting Toolkit that will make it much, much easier to deploy your SQL solutions remotely to a hosted environment.  The toolkit allows you to work with SQL Express, SQL Server 2000, and SQL Server 2005 databases locally, and then easily transfer your schema and data and install them into a shared hosting remote SQL Server account.

    The below post describes how you can start using this today. 

    SQL Server Hosting Toolkit

    The SQL Server Hosting toolkit is available for free, and ships with a Database Publishing Wizard that supports two database hosting deployment scenarios:

    1) The Database Publishing Wizard enables you to point at a database you are working with on your local system, and then automatically create a .SQL script file that contains the setup logic needed to re-create an exact replica of the database on any remote system.  This .SQL script includes everything needed to create the database schema (tables, views, sprocs, triggers, full-text catalogs, roles, rules, etc - full details here), as well as populate the new database with the same table row contents as your local database (this is analogous to the MySQL dump utility).  The benefit of having this setup logic encapsulated in a single .SQL file is that most hosters already support the ability for you to upload .SQL files to their hosted environments and run these scripts via their hosting admin control panels.  Assuming you have a web hoster that supports this today, you can immediately start using the Database Publishing Wizard to easily deploy your sites without requiring anything to be installed or configured by the hoster.

    2) The Database Publishing Wizard also enables you to point at a database you are working with on your local system, and then use web-services to transfer and recreate the database in your remote hoster environment (without you having to create the .SQL file or use the hoster admin control panel to run it).  This publishing option does require that a SQL Publishing web-service be exposed in the hosting environment, and the SQL Server Hosting Toolkit includes a free implementation of this SQL Publishing web-service that we'll be working with hosters to aggressively deploy. 

    The Database Publishing Wizard enables you to use either SQL Express or SQL Server 2000/2005 locally, and then use either SQL 2000 or SQL 2005 in the remote hoster environment.  It does not require that the versions of SQL match - so you can use SQL Express 2005 locally and then upload to a SQL 2000 server in the hosting environment without having to change any of your code.  

    The Database Publishing Wizard also supports handling the built-in ASP.NET 2.0 Membership, Role Management, Profile and Health Monitoring schemas.  A lot of people have run into issues because the built-in .SQL scripts that ship by default with ASP.NET for setting up these schemas require DBO permissions at install-time for the SQL scripts -- which a lot of hosters don't support (note: the scripts do not require DBO permissions at runtime - only for install time, but this can sometimes still be a blocker in itself unless the hoster is willing to install them for you).  The Database Publishing Wizard on the other-hand does not require DBO permissions when installing the ASP.NET Membership, Roles and Profile schemas/data, and should enable you to deploy the ASPNETDB tables + sprocs just as easily as any other database using the Database Publishing Wizard. 

    First Tutorial: Deploying a SQL Express Database to a SQL Server Hosting Account (using .SQL files)

    I'll be doing a series of posts over the next few weeks showing how to use the various features within the SQL Server Hosting Toolkit.  This first tutorial in the series covers how to use it to easily generate a .SQL installation file of a local SQL Express database that you can then copy to a remote hosting account and use to re-create a SQL Server database for you to use with your hosted site.

    Step 0: Download and Install the Database Publishing Wizard

    The first step we'll need to-do is to make sure we have the Database Publishing Wizard from the SQL Hosting Toolkit installed.  Click here to download it and install it.

    The Database Publishing Wizard comes with support for both a GUI based wizard, as well as a command-line utility.  The GUI based wizard can be run either standalone or via context-menu support that it adds to the Server Explorer in both Visual Studio 2005 and Visual Web Developer Express.  For the purposes of this tutorial we'll be using this later Server Explorer integration - which makes publishing really easy.

    Step 1: Create an ASP.NET web-site that uses a local SQL Express or SQL Server database

    To help with this demo, we will use the built-in Personal Starter Kit template that ships with both VS 2005 and Visual Web Developer Express.  To create a new web project based on it, select File->New Web Site within VWD or VS and choose the "Personal Starter Kit" template in the New Web-Site dialog.  By default the personal starter kit application is configured to use SQL Express (which is free and can be downloaded here).  When run the sample looks like below:

    After creating the application, you can then run the web admin tool (choose the WebSite->ASP.NET Configuration menu item in VWD/VS) and create a new user and add them to the "admin" role for the site.  You can then login as this new admin user and try uploading new pictures and/or customizing the existing ones on the site (note that both the picture meta-data, as well as the raw image binaries are stored in a database when you do this):

     

    Once you are all done with the above steps we'll have two SQL Express databases installed within the \app_data directory for our project.  One of the SQL Express databases is named personal.mdf and contains the tables and stored procedures specific to our web-site (photo and album tables, as well as basic content management support).  The other SQL Express database is named aspnetdb.mdf and contains the database storage for the default ASP.NET 2.0 Membership, Role and Profile providers (which the application above is using for login and admin purposes).

    Step 2: Creating .SQL Installation Scripts for our Database

    Now that we've created a new application + local database, and added custom data to it (new users + their role membership, as well as new photos and albums), we want to deploy the application to a remote hosting server. 

    The first step we'll take is to create .SQL script files that will enable us to automate re-creating the exact same database schema + database content on our remote hosting account.  To-do this we'll use the Database Publishing Wizard we installed as part of the SQL Hosting Toolkit. 

    To begin, click on the "Server Explorer" tab within Visual Studio or Visual Web Developer to see the databases that the application is using:

    As you can see in the above picture, we have two SQL Express databases that we are using: ASPNETDB.MDF and Personal.MDF.  To generate .SQL installation files for each one, simply select the database in the solution explorer, then right-click and select the new "Publish to Provider" context menu item (added by the Database Publishing Wizard) setup on it:

    This will launch the Database Publishing Wizard and allow us to walkthrough scripting the installation of our database.  As I mentioned in the intro of this blog post, the Database Publishing Wizard supports two deployment options: 1) To generate .SQL install script files that you can copy to your remote hoster and run using their existing web admin control panel tools, or 2) To upload the database directly using Database Publishing Web-Services on the hoster web-site. 

    For this first tutorial, we'll be using the .SQL script file approach - so keep the default radio button selected and provide a name for the .SQL install script file you want to generate:

     

    When you click "next" you'll be given the option to customize some of preferences when creating the .SQL setup file.  Note that you can control whether to drop existing objects within the target database, whether you want to target SQL 2000 or SQL 2005 with the script, and whether you want to setup both the schema and data, or just the schema, or just the data: 

    For this tutorial just keep the defaults selected, and hit next and generate the .SQL script:

    You now have a Personal .SQL file that contains a script that you can run on any SQL server to re-create all the tables, sprocs, views, triggers, full-text catalogs, etc. for a database, as well as import and add all of the table row data that was in the database at the time the .SQL file was created. 

    The .SQL file itself is just plain text - so you can open it up with any text editor to see it and/or customize it with your own statements:

    Notice above how the .SQL file includes both the SQL DDL needed to create the Photos table (including all of its constraints and primary-key/foreign-key relationships), as well as the SQL to insert data within the table once it is created (in the case above it is even inserting binary data for the photos - since they are stored in the database). 

    Once you repeat these steps for the ASPNETDB SQL Express database as well you'll have two .SQL installation scripts that you can use to automatically re-create your SQL database on any SQL Server:

     

    Note that the .SQL files we built can be used to create two separate databases on a server, or they can both be run against the same database to create a single database that has a unified set of tables, sprocs, and data for the application.  To accomplish this, simply run both scripts against the same database, and assuming no table or sproc names conflict, you'll have a single database containing everything.  This later option is very useful when you have a hosting account that only provides 1 database instance for you to use!

    Step 3: Using our .SQL files to create our remote databases

    Now that we have our .SQL files, we can go about using them to install our database at our hoster.  Exactly how we use the .SQL files to install the database will vary depending on how the hoster gives us access to our SQL account.  Some hosters provide an HTML based file-upload tool that allows you to provide a .SQL file - which they will then execute against the SQL database you own. 

    Other hosters provide an online query tool (like below) that allows you to copy/paste SQL statements to run against your database.  If you have a hoster which provides an online query tool like this, then you can open the .SQL file with a text-editor and copy/paste the contents into the query textbox and run it.

    The quality of the SQL tools that different hosters provide varies quite a bit.  In testing the Database Publishing Wizard we found that some custom-made SQL admin tools provided by hosters had issues where they incorrectly parsed valid SQL statements (in particular GOTO statements).  This page describes one issue you might see some hosters have with GOTO statements, along with a workaround you can use.  To help improve the overall quality of SQL hosting admin tools, the SQL Server team early next year is going to be shipping the source to a free SQL HTML admin tool that hosters will be able to integrate into their experiences.  Hopefully this will help improve the standard experience with all Windows hosters.

    If your hoster has no usable HTML web admin tool for allowing you to easily manage your SQL database, then you can also just write a simple ASP.NET page that you FTP (along with your .SQL file) to your web-site and then hit to read the .SQL file on the server in as text, and then pass it as a string to ADO.NET to execute.  This will give you the same result as the query analyzer above - and fully create your database for you.

    Step 4: Updating our connection-string within web.config

    Once we've got our data uploaded within a database at our hoster, we'll want to upload our .aspx files, assemblies and content to the remote site (typically this is done over FTP). 

    The last step we'll need to take is to open up our web.config file and update the <connectionStrings> section to point at our new database location at the remote hoster.  Note that you'll need to get the exact SQL server, database name, and username/password account to use from the hoster.

    Using our personal starter kit example above, we'd change the <connectionStrings> section within its web.config file from the default connection-string (which uses two SQL Express database in the local \app_data directory):

    <connectionStrings>
       
    <add name="Personal" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Personal.mdf" />   
       <
    remove name="LocalSqlServer"/>
       <
    add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf" />
    </
    connectionStrings>

    To instead use a single SQL Server 2000 database (the "scottguDB" database on the "Server123" box). 

    <connectionStrings>
       
    <add name="Personal" connectionString="Data Source=Server123;Initial Catalog=scottguDB;Integrated Security=True" providerName="System.Data.SqlClient" />
       <
    remove name="LocalSqlServer"/>
       <
    add name="LocalSqlServer" connectionString="Data Source=Server123;Initial Catalog=scottguDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    </
    connectionStrings>

    We were able to use a single database (instead of the two above) because we we ran both .SQL files against the single database - which merged all schema and data into a single database instance.

    Step 5: We are done

    Now we can run the application remotely in a hosted environment, and it should just work.

    Summary

    The Database Publishing Wizard that ships as part of the SQL Hosting Toolkit should make creating .SQL files for any database (SQL Express or full SQL Server) really easy.  You can use this to easily dump your local database and then use it to re-create the exact same database on a remote system.

    In future tutorials I'll also show how you can actually re-create your database remotely without even having to generate a .SQL file (instead you can publish the database directly from VS to your hoster over a web-service).  Stay tuned for details on how to-do this soon.

    Hope this helps,

    Scott

    P.S. We are planning on adding the Database Publishing Wizard into Visual Studio "Orcas" (which is the next release of VS), which means you won't need to download it separately in that timeframe.  But we wanted to make sure you didn't have to wait until then, which is why we are making it available today for Visual Studio 2005 and Visual Web Developer Express 2005.

    P.P.S. Please visit this page to find other ASP.NET Tips, Tricks and Recipes I've written on other topics in the past.

  • Tip/Trick: How to Run a Root “/” Site with the Local Web Server using VS 2005 SP1

    One of the questions I'm often asked is whether it is possible to run an ASP.NET web-site project as a top-level root "/" site using the built-in VS web-server and the VS 2005 Web Site Project model.

    By default, when you open a web-site as a file-system based web site project and run it, VS will launch and run the built-in web-server using a virtual app path that equals the project's root directory name.  For example: if you have a project named "Foo", it will launch and run in the built-in web-server as http://localhost:1234/Foo/  What a lot of people want to-do instead is to just run the web-site as http://localhost:1234/ or (if port 80 isn't already in use): http://localhost/  Doing this can make site navigation and url handling logic much simpler in your code.

    Prior to VS 2005 SP1 being released, I would recommend either running the project using IIS instead (here is a past post of mine on using the web-site project model with IIS), or to use a blog of mine from a year ago that discusses how to use the "external tools" feature in VS to enable root web-sites to accomplish this.  The good news is that VS 2005 SP1 makes this even easier with the built-in VS web-server.

    Step by Step Instructions on Configuring a VS 2005 Web Site Project to Run as a Root "/" Site

    The below steps walkthrough how to configure a VS 2005 Web-Site Project to run as a root "/" web-site:

    1) Open up an existing web-site project or create a new one by selecting the File->New Website menu item.

    2) Using the solution explorer within Visual Studio, select the web-site project node:

    3) Go to the property-grid within the IDE, which (if the project root node is selected) will now display the project properties for the web-site.  There are three relevant properties that we care about for the purposes of this tutorial: "Virtual path", "User dynamic port", and "Port Number".  Change the "virtual path" setting to / to run as a root web-site.  You can also then set the dynamic port setting to "false" and configure a specific port to use (for example: port 8081 or port 80 if it isn't already in use):

    4) Now click on a page within the project and run it.  You will see that the web-server was launched as a root "/" site:

    Note that the :8081 was added at the end of http://localhost because I already have IIS7 running on my Vista machine and it has a site using port 80.  If I disabled IIS I could have configured the web-site project to use port 80, in which case the web-browser would just have http://localhost/ in the address bar. 

    I can now perform root-relative navigation within my sitemap (example: /products, /help, etc), as well as in my redirect logic and with standard HTML elements (example: <a href="http://weblogs.asp.net/path">).  I can also now reference javascript files relative from the root, for example: <script src="http://weblogs.asp.net/js/library1.js"></script>.

    Tips/Tricks for Handling CSS StyleSheets with VS 2005 and ASP.NET 2.0

    One of the techniques I recommend taking advantage of with ASP.NET 2.0 when using CSS is to use the Master Page feature to provide a consistent UI across your site, and to reference all stylesheets in one place using a master page (all pages based on the master will then automatically pick them up). 

    One tip to take advantage of is the relative path fix-up support provided by the <head runat="server"> control.  You can use this within Master Pages to easily reference a .CSS stylesheet that is re-used across the entire project (regardless of whether the project is root referenced or a sub-application):

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>

    <html>
    <head runat="server">
        
    <title>Master Page</title>
        
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    </
    head>
    <body>
        
    <form id="form1" runat="server">
        
            
    <asp:contentplaceholder id="MainContent" runat="server">
            
    </asp:contentplaceholder>
        
        
    </form>
    </body>
    </html>

    The path fix-up feature of the <head> control will then take the relative .CSS stylesheet path and correctly output the absolute path to the stylesheet at runtime regardless of whether it is a root referenced web-site or part of a sub-application. 

    The pages within your web-site can then look like the content below and automatically pick up the stylesheet settings (both at runtime at a design-time within the VS HTML WYSIWYG Designer):

    <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Sample Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

        
    <h1>Root Web Site Sample</h1>
        
        
    <a href="/Products">Click here to go to the Products section (note the absolute path) </a>

    </asp:Content>

    Because image references contained within a .CSS stylesheet are referenced by a browser relative to the path of the .CSS file (and not actually the path of the page the stylesheet is being used on), you can combine this behavior with the <head runat="server"> logic above to have your images automatically work both with root web-sites and sub-applications (and not break if you change paths later). 

    Images referenced this way will also show up fine within the VS 2005 HTML WYSIWYG designer (which can otherwise sometimes have trouble determining the root "/" path to pick up image references).

    Hope this helps,

    Scott

    P.S. The Web Application Project Model within VS 2005 also has full support for root "/" web projects and the VS w