Hitting Breakpoints in Global.asax

Every .NET developer should know how to set breakpoints in Visual Studio 2012.

  • Right-click a line in your code, then select Breakpoint -> Insert Breakpoint.
  • Or, simply press F9 after placing your cursor on the line of code.

But, what if that line of code is in your Global.asax code? Will the breakpoint still work when you run the application? If not, what can you do to get it to work?

Breakpoint

Try the following steps:

  1. Create a new web project in Visual Studio (or open an existing web project).
  2. Open the file Global.asax.cs from your application’s root folder.
  3. Set a breakpoint in your Application_Start method.
  4. Run the application in your browser by pressing F5.

The breakpoint will most likely be skipped, because the code has already executed in IIS by the time the debugger is attached. Fortunately, there is an easy way to resolve this.

Here are some additional steps:

  1. Open the Global.asax markup file in the code editor. (See Help section below if you’re not sure how to do this.)
  2. Make an inconsequential change in it (e.g. add a space inside the closing > tag.
  3. Refresh the application in your browser.

extra space in Global.asax markup file

Results:

You should now see that the breakpoint has been hit after running the application!

Breakpoint Hit!

Need Help?

If you weren’t sure how to open the Global.asax markup file, try the following.

  1. Right-click the Global.asax file in Solution Explorer.
  2. Select View Markup from the popup context menu.

Global.asax markup file

 

2 thoughts on “Hitting Breakpoints in Global.asax

  1. Pingback: Hitting Breakpoints in Global.asax using Visual Studio | Senior DBA

  2. Wally

    In the web there are a lot of solutions for this problem but this is the only that really works! Moreover it’s also the simplest! Thank you!!!!

    Reply

Leave a Reply