Easily Serve Any Folder in IIS Express

Iisexpress7595For a long time, whenever I've been developing a site, I would always set it up in IIS with a custom host header and modify my windows hosts file to wire it all up. I know a lot of developers don't go to this extent, instead plumping to only run it via Visual Studio and Cassini. I've never been a fan of this way of working, mainly because I like to have access to my sites at all times, without having to launch Visual Studio (especially if I'm developing an Umbraco site, and all I need to do is update some CSS or something which I can do easily and quickly via the UI). The downside to setting sites up in IIS though, is that it does add extra steps to the beginning of a project, which can get tedious.

After reading a blog post by Aaron Powell, it got me thinking though. Why not use IIS Express to dynamically serve a directory as and when I need it? So with that, I knocked together a simple .vbs script you can drop in your "Send To" context menu folder (C:\Users\[YOUR_USERNAME]\AppData\Roaming\Microsoft\Windows\SendTo\), which will launch an IIS Express instance with it's path attribute set to the "Sent" folders path, and it's port attribute set to a random number between 1024 and 9999. The script will also then launch the generated URL in your default web browser. When you want to shutdown the web server, you can stop it using the IIS Express System Tray context menu.

If you'd like to try this yourself, just drop the following into a .vbs file and save it to your "Send To" folder.

' Init randomization
Randomize

' Set random port number
Dim port
port = Int(Rnd() * 8974) + 1025

' Launch IIS Express / Browser
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """%programfiles%\iis express\iisexpress"" /path:""" & WScript.Arguments.Item(0) & """ /port:" & CStr(port) & " /systray:true", 0, False
WshShell.Run "http://localhost:" & CStr(port), 9, False
Set WshShell = Nothing

UPDATE – March 15, 2011 12:25

As highlighted by Lee Kelleher in the comments, if you are using a 64bit OS, then please make sure to change %programfiles% to %programfiles(x86)%

Related Posts

User Comments

Jason

Hey Matt,

I just wanted to send a note thanking you for this nice bit of info! It is greatly appreciated.

I was in the habit of setting up every site under IIS with a host header and monkeying with the host file. That gets cumbersome and annoying quickly. Now, using IIS Express and your script I can quickly fire up any site at a moments notice.

Thanks a lot!

Jason

Javier Sanchez

Great.
I made a 2nd script called "IISExpress35.vbs" with added the parameter for v3.5 sites "/clr:3.5".
This way i have "Send To > IIS Express (4)" and "Send To > IIS Express (3.5)" to use as needed.

Matt Brailsford

Nice suggestion.

It seems to launch fairly quickly for me, but nice idea for those who have a bit of a delay.

Simon Dingley

I made one small addition to your script to allow IIS Express to fire up before launching the browser as I was getting errors in the browser because the site was not yet running.

WScript.Sleep 3000

Added after the lines that fire up IIS Express.

Daniel

Just got around to actually installing IIS Express and testing this out. Works great, very handy for quickly testing stuff you download from Codeplex.

I set mine up the same way Lee did, with a shortcut and used the icon from iisexpresstray.exe

Lee Kelleher

Ahh yes, for a moment then I'd completely forgot that people still used 32bit OSs! haha

Matt Brailsford

Nice idea, should also mean you can get rid of the .vbs extension too so looks a lot nicer.

Might go do that myself =)

Matt

Matt Brailsford

%programfiles(x86)% seems to only work on a 64bit OS (which I'm running 32bit), but I've added a comment to the post for people using 64bit like yourself.

Thanks for the comment.

Matt

Lee Kelleher

Good thinking with the SendTo folder!

I decided to put the VBScript in a different location, then link up a shortcut to it - that way I could customise the icon.

Posted my version up on GitHub:
https://gist.github.com/870634

Lee Kelleher

As far as I know, IIS Express is an x86 application, so using %ProgramFiles(x86)% would be better?

Matt Brailsford

That was easy =)

Updated the post. Thanks for your suggestion.

Matt

Matt Brailsford

Good call. Shouldn't be too difficult to update it.

Matt

Daniel

Great idea.

Maybe it would be good practice to retrieve the %programfiles% system environment variable and use that instead of the DOS folder name and hard-coded drive letter though.

comments powered by Disqus