Update: IIS Express has been officially released as a standalone installation.

For those of us too impatient to wait for Visual Studio SP1 to natively support IIS Express, I've done some digging and found a way to [fairly] easily setup a debugging environment for IIS Express and VS 2010 (should work for VS 2008 also, though!). This assumes you are at least an intermediate user of .NET/IIS and Visual Studio.

Prerequisites

  1. Download and install IIS Express.
  2. Be using Visual Studio 2010 or 2008 and a web project to debug.

Steps to Setup IIS Express

It's actually quite simple to setup IIS express. Once IIS Express is done installing, go to "My Documents\IISExpress8\config".

image

Right-click "applicationhost.config" and open in your favorite text editor.

Go to line 145. Notice the <sites> element. This is where we can configure our website for IIS Express. Copy the site that is already there and add another entry below it.

Remove the "autoStart" attribute for the first site.

image

This should be self-explanatory, but in case it isn't, we are setting up a root site in IIS:

  • "name" is what you want the website name to be. This can be whatever you want.
  • "id" is just the ID of the site for IIS Express. Increment it.
  • "physicalPath" is the directory your site files are located in, i.e. the root folder of the website. Mine is below:

    image
  • "bindingInformation" is where you set the host name and port you want the site to run in. This can be whatever you need it to be, perhaps if you have a static port set on your VS project.
  • "applicationPool" is the app pool to run under. This is important! If your site is .NET 4, you can use "Clr4IntegratedAppPool". If you are .NET 3.5 or below, you must use "Clr2IntegratedAppPool". Otherwise you will get config errors.

Let's test it out! Save the config file and close it.

Go to Run… and type in the following command:

"C:\Program Files (x86)\Microsoft WebMatrix\iisexpress.exe" /site:{YOUR_SITE_NAME}

Replace "{YOUR_SITE_NAME}" with the name you gave the site in the config file above. If you are on 32-bit windows, omit the " (x86)" part after Program Files.

Side Note: You can get a list of command-line switches by going into Command Prompt and typing in "iisexpress /?". You'll notice you can also directly launch any site by physical path and desired port.

Now open your browser and browse to the URL of your site! Voila, it should work (or not, in my case… but that's because this isn't my dev machine):

image

You might want to create a shortcut on your desktop to the run command I had you type in earlier. It will make it a lot easier to start IIS Express when you're developing.

Note: Now that you've set up this web site, you could manage it from the WebMatrix GUI if you prefer that method for more advanced settings.

Setting up Visual Studio 2010/2008

Go ahead and open up your solution in Visual Studio.

Right-click on your web project and go to Properties, then the Web tab. Click the "Start URL:" radio button and put in the path to your site:

SnipImage

Then below it, click the "Use Custom Web Server:" radio button and again put the path to the URL:

SnipImage (2)

Next, and this is important, uncheck everything below it. Visual Studio will try to attach to the non-existent IIS server otherwise and not let you debug. We're sort "tricking" it into using IIS Express.

Save and close the project's properties. Now hit Alt-F8 to open up Macro Explorer. Right-click on the My Macros –> Module 1 node and click "Add Macro."

macro_explorer

Use the following macro:

SnipImage (3)

Source:

Public Sub AttachToIISExpress()
    For Each process As EnvDTE.Process In DTE.Debugger.LocalProcesses
        If (process.Name.IndexOf("iisexpress.exe") <> -1) Then
            process.Attach()
        End If
    Next
End Sub

 

That is it. The process to follow is:

  1. Start IIS Express using the run command/shortcut above.
  2. Hit F5 to start debugging. Visual Studio will enter debug mode and start the site in a browser window.
  3. Next, run the macro we created to attach to the IISExpress server.
  4. Voila, you now can debug not only your solution's code but also any scripts loaded by the browser!

SnipImage (4)

Setting up a Keyboard Shortcut

If you're like me, right-clicking and running the macro is too much work! So let's create a keyboard shortcut, shall we? How about 'F4'?

Go to Tools –> Customize… then click the "Commands" tab. Select "Debug" in the "Menu bar:" drop-down.

SnipImage (5)

Click "Add Command…" and click "Macros" in the left-hand list. Click the IISExpress macro.

SnipImage (6)

Now just rearrange the list to your liking. You can click the newly added command and then click Modify Selection –> Rename.

Click Close and close the customize dialog.

Go to Tools –> Options. Click Keyboard in the left-hand list. Start typing "view.prop" to find the Properties Window keyboard shortcut (already assigned to F4). Hit Remove to get rid of the F4 shortcut.

SnipImage (7)

Now type in "iis" to filter down to the IIS macro. In the shortcut keys box, hit F4, then Assign.

SnipImage (8)

All done! Now instead of right-clicking the macro every time, you only need to hit F4 after you hit F5.

Closing Comments

Remember that this is just a temporary workaround until Scott and his ninja team release a hotfix for Visual Studio to enable native IIS Express integration. I needed this functionality this weekend while working on a project (due to how Cassini handled URL requests and because attaching to IIS 7 for debugging was not working for me).

I hope this helps some other fellow curious developers. As always, leave a comment if you have a suggestion to streamline this even more (I attempted to wrap the whole debug process into one macro with no success… maybe someone smarter than me can!).