Silverlight Tools Tip: How to customize the default Hosting Web Project Choices for Silverlight Applications

Currently in Silverlight Tools Beta 2, by default when you create a new Silverlight Application, Web Site is the default project type for hosting the Silverlight app, and Web Applications are the only other option.  However, you might either prefer to use a custom web project template, or maybe just want Web Applications to be the default.  Let’s look at how to customize this setting:

When you create a new Silverlight project in VS2008, you’re greeted with a dialog where you can create a web project in which to host your Silverlight app.  The (probably familiar) dialog looks like this:

image 

With only Silverlight Tools for Visual Studio installed, the only options in the drop down are Web Site and Web Application.  This drop down is customizable through editing the system registry.

First, let’s look at how to change the order of these hosting options.  If you fire up the Registry Editor, and look under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Packages\{CB22EE0E-4072-4ae7-96E2-90FCCF879544}\SupportedWebProjects you’ll see the list of options for hosting your Silverlight application.  The keys under this have the following structure:

 [Key Name]
   (Default) [REG_SZ]    = Name of the web project
   SortIndex [REG_DWORD] = Value with which to sort (ascending order from 0-100)
   - [Language key (e.g. C#, VB)]
       (Default) [REG_SZ] = Path to project template
   - [Language key (e.g. C#, VB)]
       (Default) [REG_SZ] = Path to project template

Unfortunately, in Beta 2, we had a bug where the SortIndex value wasn’t being written for WebSites and WebApplications.  However, you can add them in yourself.  For example, here’s how the keys for WebApplication might look when I give it a SortIndex:

 [WebApplications]
   (Default) [REG_SZ]    = #1015
   SortIndex [REG_DWORD] = 0x0000000a (10)
   - [C#]
       (Default) [REG_SZ] = CSharp\Web\{lcid}\WebApplicationProject.zip\WebApplicationProject.vstemplate
   - [VB]
       (Default) [REG_SZ] = VisualBasic\Web\{lcid}\WebApplicationProject.zip\WebApplicationProject.vstemplate

The name for WebApplications might look odd (#1015), but that’s because it’s a resource ID in one of our assemblies.  If you provide a custom name for your own, you can just put the name you want to appear.  Here’s a sample one I made up to demonstrate, using the WAP template, but otherwise custom values (I put SortIndex lower so it would appear first in the list):

 [CustomWebType]
    (Default) [REG_SZ]    = This is my custom web template
    SortIndex [REG_DWORD] = 0x00000008
    - [C#]
        (Default) [REG_SZ] = CSharp\Web\{lcid}\WebApplicationProject.zip\WebApplicationProject.vstemplate

If I start up VS again, we can see the list has been changed to contain my new template:

image

You can also see MVC Web Application Project in the list.  If you’re considering ASP.NET MVC development and want to throw some Silverlight spice into your web site, you can use the ASP.NET MVC Preview 4 from Codeplex and you’ll have this come up in your list (note: due to the missing SortIndex for WAP and Website, the MVC option will end up first in the list when you first install; I’ve customized my order before taking this screenshot).

A couple last notes: you may have noticed that I only defined a C# template for my custom hosting web project.  If I were to create, for example, a VB Silverlight Application, then my template would still show up in the list.  If I create the projects, I’ll end up with a solution containing my VB Silverlight Application and a C# Web Application to host it in.  If instead of C#, I put a language that doesn’t even have a matching Silverlight template (such as C++), it will still show up in the list.  If the project template that you give is not a web project type (e.g. a Windows client app template), the operation to create the Silverlight app and your custom app will fail (since it also tries to link them).