Using IsDebuggingEnabled in ASP.NET

A cool feature in the .NET library that I rarely see used is the IsDebuggingEnabled property. This returns a value indicating whether the current HTTP request is in debug mode. I’ve found this useful in the past where I wanted something displayed on the screen to distinguish if the website I’m working on is a debug build or not. To change the compilation of a website, you update the compilation element in the web.config file:

Debugging is off: <compilation debug="false">
Debugging is on: <compilationdebug="true">

The following code demonstrates how to use this property. When page is loaded and debugging is enabled, a JavaScript alert will be displayed:

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head id="Head2" runat="server">
<
title></title>
<
script language="javascript" type="text/javascript"
src="http://code.jquery.com/jquery-latest.js"></script>
<
script language="javascript" type="text/javascript">
$(document).ready(function() {
if ('<%= System.Web.HttpContext.Current.IsDebuggingEnabled %>' == 'True') {
alert("Debugging enabled!");
}
});
</script>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>

</
div>
</
form>
</
body>
</
html>

I find this piece of code very useful if I want to see results that I would not want to be displayed, in a production environment.

No comments:

Post a Comment