Debugging ASP

Yeah, I’ve Done That:

Debugging ASP with Visual Studio 2008 and IIS 7.5

Updated: 11/24/09

So it’s 2009 and I have this client with a website implemented in ASP. Well, we can’t all jump right onto Microsoft’s many releases of new technologies, and when something works it’s hard to rationalize spending a lot of money to port an entire code base to ASP.NET. This little article describes how to configure Visual Studio and IIS to allow you to maintain, enhance, and debug an ASP website. And all on Windows 7. Who says we aren’t keeping up with Microsoft technologies? I will say that these tools provide a far richer and more stable platform than Visual InterDev ever did!

Environment

    • Windows 7

    • Visual Studio 2008 (a/k/a 9.0)

    • IIS 7.5

    • Solution and project files on a network share

The ASP Source Files

I am going to assume that you do not already have a Visual Studio project file for your website’s ASP code. And I’m going to assume you want to create your Visual Studio solution in a directory called

H:\vsprojects\MyWebsiteSolution

The H: drive is a network share and you will need to know the UNC path for this directory as well. For example:

\\S1\gd\vsprojects\MyWebsiteSolution

The ASP website code will live in a subdirectory of this directory named MyWebsite. Go ahead and create these two directories. Then find your ASP code files and place them in the MyWebSite subdirectory.

Create the Solution File

Fire up Visual Studio 2008 (while writing this article I used the Development Edition which is stamped with the version number 9.0.30729.1 SP, oh, and with the .NET Framework 3.5 SP1). Select FileOpenWeb Site. Browse to the following directory:

H:\vsprojects\MyWebsiteSolution\MyWebsite

and click the Open button.

You should see all your ASP code files in the Solution Explorer view. Immediately go to the File menu and select Save All (Ctrl+Shift+S). You will be prompted so save the new solution file somewhere. Visual Studio is going to suggest that you save the file in the directory H:\vsprojects\MyWebsite\MyWebsite.sln. Don’t! You want the solution file placed in the H:\vsprojects\MyWebsiteSolution directory, which is the parent directory to where your new website project now lives. So navigate to this directory and save the solution file there.

Now close Visual Studio (it will probably prompt you to save the changes made to the solution file; if it does go ahead and click the Yes button) then navigate to the solution file and double-click it to open it up again.

Note: At this point, even though you did not save the solution file in the location that Visual Studio suggested, Visual Studio went ahead and created that suggested directory anyhow. Go ahead and remove that directory if you want.

You now have your website’s ASP code all packaged up in a nice Visual Studio solution. Maybe you would like to right-click the website project and select Add Web Deployment Project to give you a clean and simple way to deploy your ASP app. And you might want to add a nice new C# class library project so you can start writing .NET code that you can use in your ASP app. (Check out my Calling .NET Code From ASP article for details on doing that.) But I’m getting ahead of myself, back to ASP.

Create the IIS Application

Note: Be sure that you enter the UNC path here and do not use the mapped drive letter name because the IIS worker process does not have your network share names mapped.

Since the project files are stored on a network share, it is necessary to create an IIS application pool using your Windows user account for the worker process so that IIS will be able to access the share and the files. So fire up IIS Manager. In the Connections view, click the Application Pools node. In the Actions pane, click Add Application Pool link. Give the pool a name (I use gdpool) and click the OK button. Highlight the new pool entry, then click the Advanced Settings link in the Action pane. In the Process Model section, change the Identity field from NetworkService to your Windows user account, and set the Enable 32-Bit Applications option to True (ASP is 32-bit only) and the Managed Pipeline Mode to Classic. Click the OK button.Now create the website itself. Navigate to your default website node under the Sites node in the Connections view. Right-click the default website node and select Add Application. Enter an alias name (maybe mywebsite) and in the Physical path field put the website directory:\\S1\gd\vsprojects\MyWebsiteSolution\MyWebsite

Click the Connect As button then enter your Windows user account information. Click the OK button.Highlight your newly create website node and click the Advanced Settings link in the Actions pane. Change the Application Pool entry to the pool you just created, then click the OK button.Since this is an ASP website, you will probably want to use a default document like default.asp or index.asp so highlight the website’s node under the default website node then double-click the Default Document feature. Add the appropriate default document names here.

The default document at the root of the website should be displayed.Debugging

Back to Visual Studio. Open the solution. Right-click the solution node and select Start Options. Change to a custom web server and set the base URL to:http://localhost/mywebsite/

Set the ASP debugging properties for the site by highlighting the website’s node under the default website node, then double-click the ASP feature. Expand the Debugging Properties section, then set the Enable Client-side Debugging, the Enable Server-side Debugging, and the Send Errors To Browser options to True. Be sure to click the Apply link in the Actions pane when finished.You should now be able to browse to your website. Enter this URL into your favorite web browser:http://localhost/mywebsite/

Also be sure that the Native code and SQL Server debuggers are selected. Click the OK button to close the Property Pages window.Go ahead and build the solution now. Since this is an ASP app. no DLLs will be created. But you will need to be able to successfully build your app. in order to start the debugger. I ran into an issue with one of my ASP projects because it had a few files ending in .java in it (I found the answer to this particular issue over at the .NET Slackers website). So do a build to make sure you don’t get any errors.

Enable debugging in your web.config file. You should have a debug setting like this:

<system.web>

<compilation debug="true"/>

</system.web>

Now you can right-click one of the ASP files and choose View in Browser. If there are any errors with displaying the page then the Visual Studio Just-In-Time Debugger will pop up and you can select the MyWebsite instance to debug with. After choosing the instance of Visual Studio to use a dialog labeled Attach Security Warning will be displayed informing you that it can be dangerous to attach to the process (whatever that means), just click the Attach button. Visual Studio will go to the source file with the error in it and also display a pop-up window with the error message in it and the option to Break, Continue, or Ignore the error. Of course, click the Break button. The Locals debugger window will have all of your variables that you can inspect to help figure out the problem.

Once the solution builds without any errors and you are able to view one of your ASP files in a browser, you are ready for a normal debugging session. Select a page in your website that you know will run without any errors. Right-click that page and choose Set As Start Page. Open up the ASP page you want to debug and set a breakpoint in it. Then use the Start Without Debugging command on the Debug menu (or just press Ctrl+F5). Your start page will be displayed in your browser. Back in Visual Studio, select Attach to Process from the Debug menu. Make sure that the Show processes in all sessions option is selected then look in the Available Processes list and find the w3wp.exe process. It is usually easiest to click the Process column header to do a descending sort to bring that process right near the top of the list. The Type column for this process should have the word Script in it. Highlight the w3wp.exe process then click the Attach button. The Attach Security Warning dialog will appear; click the Attach button. Now go back to the browser window and refresh it (F5) and your breakpoint will be hit.

To summarize that last rather long paragraph, here’s the steps to debug an ASP page:

    1. Select an error-free page as your start page.

    2. Set your breakpoint.

    3. Press Ctrl+F5.

    4. Attach to the w3wp.exe process.

    5. Refresh your browser window.

Enjoy your debugging!

Don’t Forget .NET

The real beauty to all of this is that you can also add .NET code to your ASP website. Now that you have everything housed in Visual Studio 2008, just right-click the project node and select Add New Item. Select the Web Form template, type in a name like test.aspx, select C# for the language (of course), then click the Add button. Throw a text box control on the page, fill in the control’s Text property from the code-behind file, then right-click and select View in Browser. Voila! Now set a breakpoint in the code-behind file, click F5, and bang! you’re debugging your .NET page. No running an error-free page to get the worker process started and no manually attaching to the worker process. Now convert your ASP pages to .NET one by one!

References

Rock Webcast (pardon me?) over at the Code Project wrote up a great article titled Debugging Classic ASP (VBScript) in Visual Studio 2008. This article was invaluable to me in shaking up those old memory cells on the nuances of configuring ASP for debugging. But his article describes IIS version 6 and I want to use IIS 7.5 (hey, if I have to use ASP/VBScript at all I at least want to use the latest and greatest Microsoft web server). The comment posted in the article titled Need to do it slightly different under Server 2008 also has excellent insight into the process.

Feedback

Please leave comments about this article on my blog!