How to Debug Your ASP.NET Projects Running Under IIS

If you've done any coding in ASP.NET then you know how important debugging is. By default Visual Studio comes with its own stripped down web service that it runs every time you debug your application in order to serve up pages. 90% of the time that little web server is not powerful or fast enough to keep up with an enterprise level application. A lot of companies will have you map your site to [Internet Information Services][IIS] which is a proper web server that comes with Windows. IIS has a lot more options and is more robust for application hosting (understandably since that is its whole purpose) whereas the little web service that starts in the default configuration of visual studio hardly compares.

The downside of having your application run by IIS is that it is not immediately apparent how to debug the application. Pressing F5 still tries to launch the dinky web service that comes with Visual Studio. Many people quickly figure out that they can attach to the process thread started by IIS for the application it is running. It's usually named something like "w3wp.exe". You do this by going to the "debug" menu in Visual Studio and then selecting "Attach to Process". This brings up a little window with a list of running processes. Finding the IIS process for your application and clicking attach will then start Visual Studio's debugger. You can then hit the pages hosted by IIS and hit your breakpoints in your code.

While this is fine and works, it's a giant pain to go to Debug > Attach to Process every single time you need to debug your application. You could create a macro that would do the work for you, but why write a macro when Visual Studio will actually do it for you. Let me show you how to set up your project to use IIS by default when debugging. You won't ever have to navigate your process tree again.

There are two types of web projects in .NET, Websites and Web Applications. There is much debate about which one is better for development, but this post is not about that so I will not get into it here. However, these two project types have different properties menus and configuring them to use IIS when debugging is slightly different in each of them. For simplicity's sake I decided to break it up into two sections, one for web apps, and one for websites. Please see the section that corresponds to your project type. (If you are using ASP.NET MVC then you are using a web application)

Websites

If your project is a website and not a web app, then here are the steps to configure it to use IIS when debugging. This assumes you already have IIS set up and hosting your project.

  1. First open up your project and open the solution explorer.
  2. Right-click on your project node and navigate to "Property Pages".
  3. Navigate to "Start Options" item in the left pane.
  4. In the "Server" section make sure "Use custom server" is checked.
  5. In the "Base URL:" field put in the address you have mapped to your project. (Usually the address you put in your hosts file)
  6. You're basically done, but another option I like to set on this page is "Don't open a page. Wait for request from an external application." I set this because I don't like closing a million browser tabs for every time I debug. I usually just leave my browser open behind visual studio and when I debug I prefer to just switch to the browser and refresh rather than have Visual Studio open a new tab.

Web Applications

If your project is a web application and not a website, then here are the steps to configure it to use IIS when debugging.

  1. First open up your project and open the solution explorer.
  2. Right-click on your project node and navigate to "Properties".
  3. Find the "Web" tab on the left-hand side.
  4. Under the "Servers" section select "Use Local IIS Web Server".
  5. This next step varies by how you have your project set up.
    • If you have your project mapped to IIS already then simply put the local URL in the "Project Url" field.
    • If you have NOT mapped your project to IIS yet then it is usually more convenient to click the "Create Virtual Directory" button and let it do it for you. (If you have not set up IIS on your machine correctly then this will fail) Note: Keep in mind that this is not the same as creating a new website in IIS. It creates a virtual directory under the default website that comes with IIS. This can cause problems if you were bad and you used application relative paths instead of absolute paths as root relative paths refer to the website root, not the virtual directory root. Learn how to make your web project build absolute paths based on an application relative root path using the tilde (~).
  6. You're basically done, but another option I like to set on this page is "Don't open a page. Wait for request from an external application." I set this because I don't like closing a million browser tabs for every time I debug. I usually just leave my browser open behind visual studio and when I debug I prefer to just switch to the browser and refresh rather than have Visual Studio open a new tab.

Chev

Read more posts by this author.

comments powered by Disqus