03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

712 x CHAPTER 18 EXCEPTION HANDLING, DEBUGGING, AND TRACING<br />

make it easy to set breakpo<strong>in</strong>ts, VS doesn’t restrict you to add<strong>in</strong>g them <strong>in</strong> pages at design time only;<br />

<strong>in</strong>stead, it also enables you to set them <strong>in</strong> the dynamic runtime files. When you stop debugg<strong>in</strong>g, VS<br />

tracks the new breakpo<strong>in</strong>ts for you, f<strong>in</strong>ds out to what source file they belong, <strong>and</strong> adds them there aga<strong>in</strong><br />

for you, so they are available for the next debugg<strong>in</strong>g session.<br />

Although you may not realize it because everyth<strong>in</strong>g is tak<strong>in</strong>g place on the same computer <strong>and</strong> <strong>in</strong> the<br />

same IDE, you are cross<strong>in</strong>g many boundaries when debugg<strong>in</strong>g like this. First, VS enables you to debug<br />

client-side script <strong>in</strong> the browser, so you can hook <strong>in</strong>to that even before any data is sent to the server.<br />

When you press F5 <strong>in</strong> step 8, the code cont<strong>in</strong>ues <strong>and</strong> sends the value to the server where it was used <strong>in</strong><br />

the HelloWorld method of the NameService class. Once that server-side web method is done, execution<br />

returns to the client aga<strong>in</strong>, enabl<strong>in</strong>g you to break on the alert statement that shows the message<br />

from the web service.<br />

For some reason, debugg<strong>in</strong>g client-side JavaScript <strong>in</strong> VS 2012 doesn’t always work. If you’re encounter<strong>in</strong>g<br />

issues, remember the debugger keyword. Just add it before the l<strong>in</strong>e you want to break on <strong>and</strong> VS<br />

will halt execution when it encounters this keyword. Don’t forget to remove the debugger keyword<br />

aga<strong>in</strong> if you’re done with debugg<strong>in</strong>g; otherwise, your browser will try to start a script debugger when it<br />

encounters this keyword, which is mean<strong>in</strong>gless to most of your users.<br />

So far you’ve been look<strong>in</strong>g at debugg<strong>in</strong>g code. However, VS now has great support to diagnose the<br />

HTML of your page as well. You see how this works next.<br />

DEBUGGING WITH THE PAGE INSPECTOR<br />

If you want to build a site that is easy to ma<strong>in</strong>ta<strong>in</strong>, you probably make use of many of the features<br />

that <strong>ASP</strong>.<strong>NET</strong> <strong>and</strong> the browser offers. For example, you’re probably us<strong>in</strong>g a master page for the<br />

general layout, you store page-specific content <strong>in</strong> a content page that uses that master page, you may<br />

have one or more user controls for content you reuse across your site (such as the Contact Form <strong>in</strong><br />

the Planet Wrox website), <strong>and</strong> you may be us<strong>in</strong>g themes <strong>and</strong> sk<strong>in</strong>s to separate the design from the<br />

rest of your application.<br />

In the browser, all of this code comes together as a s<strong>in</strong>gle HTML source document that <strong>in</strong> turn<br />

<strong>in</strong>cludes references to external resources such as JavaScript <strong>and</strong> CSS files <strong>and</strong> images. Because it’s a<br />

s<strong>in</strong>gle file, it can be hard to debug that code <strong>and</strong> underst<strong>and</strong> where a certa<strong>in</strong> piece of HTML came<br />

from. In addition, because multiple CSS selectors can <strong>in</strong>fluence the layout of your page, it may be<br />

hard to track down the file <strong>in</strong> which a certa<strong>in</strong> piece of CSS is def<strong>in</strong>ed. Us<strong>in</strong>g the Page Inspector, new<br />

to VS 2012, this now becomes much easier.<br />

Introduc<strong>in</strong>g the Page Inspector<br />

The Page Inspector is a diagnostics tool that runs <strong>in</strong>side Visual Studio <strong>and</strong> that br<strong>in</strong>gs a unified<br />

experience between your browser, the <strong>ASP</strong>.<strong>NET</strong> run time, <strong>and</strong> your source files. The Page Inspector<br />

comes with Visual Studio so you don’t need to do anyth<strong>in</strong>g to make use of it.<br />

Although an upcom<strong>in</strong>g exercise shows you many of the features that the Page Inspector offers, here’s<br />

a quick description of how you can use it:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!