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.



Will you give this article a +1 ? Thanks in advance


About The Author

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET and regular presenter at conferences and user groups throughout Australia. Being an ASP.NET Insider, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with JavaScript. He also blogs regularly at DotNetCurry.com. Follow him on twitter @malcolmsheridan

No comments: