<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Egil Hansen &#187; ASP.NET</title>
	<atom:link href="http://egilhansen.com/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://egilhansen.com</link>
	<description></description>
	<lastBuildDate>Thu, 09 Sep 2010 08:04:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Simple Ping/Reply Service for Unit Testing AJAX/XHR requests</title>
		<link>http://egilhansen.com/2010/09/09/simple-pingreply-service-for-unit-testing-ajaxxhr-requests/</link>
		<comments>http://egilhansen.com/2010/09/09/simple-pingreply-service-for-unit-testing-ajaxxhr-requests/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 07:58:13 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://egilhansen.com/2010/09/09/simple-pingreply-service-for-unit-testing-ajaxxhr-requests/</guid>
		<description><![CDATA[To make it easier for me to unit test a OData JavaScript library I am working, I created a small HTTP handler that will reply back to a AJAX request with the headers, HTTP verb and query string it received. My initial thought was to do this through a WCF service, and while this is [...]]]></description>
			<content:encoded><![CDATA[<p>To make it easier for me to unit test a <a href="http://github.com/egil/ODataJS">OData JavaScript library</a> I am working, I created a small HTTP handler that will reply back to a AJAX request with the headers, HTTP verb and query string it received. My initial thought was to do this through a WCF service, and while this is certainly possible, I found it much easier just to create a simple HTTP handler.</p>
<p>The code for the <strong>AjaxPingHandler.cs</strong> handler looks like this:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; height: 349px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">using</span> System.IO;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">using</span> System.Linq;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">using</span> System.Runtime.Serialization.Json;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">using</span> System.Web;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #0000ff">namespace</span> ODataJS.services</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AjaxPingHandler : IHttpHandler</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ProcessRequest(HttpContext context)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>             var request = context.Request;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>             var response = context.Response;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>             <span style="color: #008000">// serialize PingReply object</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>             <span style="color: #0000ff">string</span> jsonText = <span style="color: #0000ff">string</span>.Empty;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>             <span style="color: #0000ff">using</span> (var ms = <span style="color: #0000ff">new</span> MemoryStream())</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>                 var jsonSerializer = <span style="color: #0000ff">new</span> DataContractJsonSerializer(<span style="color: #0000ff">typeof</span>(PingReply));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>                 jsonSerializer.WriteObject(ms, <span style="color: #0000ff">new</span> PingReply(request));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>                 jsonText = Encoding.Default.GetString(ms.ToArray());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>             <span style="color: #008000">// set content type to json</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>             response.ContentType = request.AcceptTypes.Contains(<span style="color: #006080">&quot;application/json&quot;</span>) ? <span style="color: #006080">&quot;application/json&quot;</span> : <span style="color: #006080">&quot;text/javascript&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>             <span style="color: #008000">// wrap in d object to avoid script injection</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>             jsonText = <span style="color: #006080">&quot;{\&quot;d\&quot;:{&quot;</span> + jsonText.Substring(1, jsonText.Length - 1) + <span style="color: #006080">&quot;}&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>             <span style="color: #008000">// output content</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>             response.Write(jsonText);            </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsReusable</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>             <span style="color: #008000">// To enable pooling, return true here.</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             <span style="color: #008000">// This keeps the handler in memory.</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>             get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see, it uses the native .net <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx">DataContractJsonSerializer</a> to serialize a custom class called PingReply. The ContentType return header is determined by the AcceptTypes request header. If the AcceptTypes array contains &quot;application/json&quot; the ContentType header is set to the same, otherwise we set the ContentType header to &quot;text/javascript&quot;.</p>
<p>Just for good measure, the result data is wrapped in a dummy object { &quot;d&quot; : . }, just like .net&#8217;s JSON enabled services does it. It is not because I am terrible worried about script injection attacks when doing unit tests, but it makes the service behave more like other .net JSON services, which makes things consistent.</p>
<p>The <strong>PingReply.cs</strong> class:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">using</span> System.Linq;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">using</span> System.Runtime.Serialization;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">using</span> System.Web;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">namespace</span> ODataJS.services</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     [DataContract]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> PingReply</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         [DataMember]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> HTTPVerb { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         [DataMember]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         <span style="color: #0000ff">public</span> JsonDictionary Headers { get; <span style="color: #0000ff">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         [DataMember]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         <span style="color: #0000ff">public</span> JsonDictionary QueryString { get; <span style="color: #0000ff">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         <span style="color: #0000ff">public</span> PingReply(HttpRequest request)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>             Headers = <span style="color: #0000ff">new</span> JsonDictionary();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>             QueryString = <span style="color: #0000ff">new</span> JsonDictionary();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>             <span style="color: #0000ff">foreach</span> (var key <span style="color: #0000ff">in</span> request.Headers.AllKeys.Where(x =&gt; x != <span style="color: #0000ff">null</span>))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>                 Headers.Add(key, request.Headers[key]);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>             <span style="color: #0000ff">foreach</span> (var key <span style="color: #0000ff">in</span> request.QueryString.AllKeys.Where(x =&gt; x != <span style="color: #0000ff">null</span>))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>                 QueryString.Add(key, request.QueryString[key]);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>             HTTPVerb = request.HttpMethod;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The only thing interesting about the PingReply class is that it uses the JsonDictionary class to return the headers and query string instead of the native .net Dictionary collection class. I decided to use the JsonDictionary class because it serializes to a proper JavaScript object, and not a array of one-element objects like the .net Dictionary. <a href="http://social.msdn.microsoft.com/profile/carlos%20figueira/">Carlos Figueira</a> first published the JsonDictionary class over at the <a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/8bef40bc-8466-4c6f-a717-15f3d6e61e3c">msdn forums</a>.</p>
<p>The <strong>JsonDictionary.cs</strong> class:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">using</span> System.Runtime.Serialization;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">namespace</span> ODataJS.services</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     [Serializable]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> JsonDictionary : ISerializable</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         <span style="color: #0000ff">readonly</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt; _dict = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         <span style="color: #0000ff">public</span> JsonDictionary() { }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         <span style="color: #0000ff">protected</span> JsonDictionary(SerializationInfo info, StreamingContext context)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>             <span style="color: #0000ff">foreach</span> (var entry <span style="color: #0000ff">in</span> info)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>                 _dict.Add(entry.Name, entry.Value);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> GetObjectData(SerializationInfo info, StreamingContext context)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>             <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> key <span style="color: #0000ff">in</span> _dict.Keys)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>                 info.AddValue(key, _dict[key], _dict[key] == <span style="color: #0000ff">null</span> ? <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">object</span>) : _dict[key].GetType());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Add(<span style="color: #0000ff">string</span> key, <span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>             _dict.Add(key, <span style="color: #0000ff">value</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>To make it all work, you simply need to add the following to your <strong>web.config</strong> file:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">configuration</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>   <span style="color: #0000ff">&lt;</span><span style="color: #800000">system.web</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">httpHandlers</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">verb</span><span style="color: #0000ff">=&quot;*&quot;</span> <span style="color: #ff0000">path</span><span style="color: #0000ff">=&quot;AjaxPing.ashx&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>         <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;ODataJS.services.AjaxPingHandler&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">httpHandlers</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">system.web</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>   <span style="color: #0000ff">&lt;</span><span style="color: #800000">system.webServer</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">handlers</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">verb</span><span style="color: #0000ff">=&quot;*&quot;</span> <span style="color: #ff0000">path</span><span style="color: #0000ff">=&quot;AjaxPing.ashx&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;AjaxUnittestService&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;ODataJS.services.AjaxPingHandler&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">handlers</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">system.webServer</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">configuration</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>Then it is just a matter of querying AjaxPing.ashx and unit test against the reply from the service. </p>
<p>I hope this helps others who also want to produce better JavaScript code.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2010/09/09/simple-pingreply-service-for-unit-testing-ajaxxhr-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to take control of style sheets in ASP.NET Themes with the StylePlaceholder and Style control</title>
		<link>http://egilhansen.com/2008/11/30/how-to-take-control-of-style-sheets-in-aspnet-themes-with-the-styleplaceholder-and-style-control/</link>
		<comments>http://egilhansen.com/2008/11/30/how-to-take-control-of-style-sheets-in-aspnet-themes-with-the-styleplaceholder-and-style-control/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 11:22:49 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.egil.dk/?p=83</guid>
		<description><![CDATA[The problem is pretty simple. When using ASP.NET Themes you do not have much say in how your style sheets are rendered to the page. The render engine adds all the style sheets you have in your themes folder in alphabetic order, using the &#60;link href=&#8221;&#8230;&#8221;? notation. We all know the order of the style [...]]]></description>
			<content:encoded><![CDATA[<p>The problem is pretty simple. When using ASP.NET Themes you do not have much say in how your style sheets are rendered to the page.</p>
<p>The render engine adds all the style sheets you have in your themes folder in alphabetic order, using the &lt;link href=&#8221;&#8230;&#8221;?  notation.</p>
<p>We all know the order of the style sheets are important, luckily asp.net&#8217;s shortcomings can be circumvented by prefixing the style sheets with 01, 02, &#8230; , 99, and thus forcing the order you want (see Rusty Swayne <a href="http://rustyswayne.com/post.aspx?id=cba116ea-b672-4c90-9f4e-18b70ca2f50a">blog post</a> on the technique for more information).</p>
<p>This is especially important if you use a reset style sheet, which I highly recommend; it makes it much easier to style a site in a consistent form across browsers (take a look at <a href="http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/">Reset Reloaded from Eric Meyer</a>).</p>
<p>You also miss out of the possibility to specify a media type (e.g. screen, print, projection, braille, speech). And if you prefer to include style sheets using the @import method, you are also left out in the cold.</p>
<p>Another missing option is Conditional Comment, which is especially useful if you use an &#8220;ie-fix.css&#8221; style sheet.</p>
<p>Before I explain how the StylePlaceHolder and Style control resolve the above issues, credit where credit is due, my solution is inspired by <a href="http://www.dentaku.com/2007/01/take-control-over-stylesheet-order-and-media-when-using-asp-net-2-0-themes.aspx">Per Zimmermann&#8217;s blog post</a> on the subject.</p>
<p>The StylePlaceHolder control is placed in the header section of your master page or page. It can host one or more Style controls, and will remove styles added by the render engine by default, and add its own (it will only remove styles added from the current active theme).</p>
<p>The Style control can both host inline styles in-between it&#8217;s opening and closing tags and a reference to a external style sheet file through its CssUrl property. Through other properties you control how the style sheet it renders to the page.</p>
<p>Let me show an example. Consider a simple web site project with a master page and a theme with three style sheets &#8211; 01reset.css, 02style.css, 99iefix.cs. Note: I have named them using prefixing technique described earlier, as it makes for a better design time experience. Also, the tag prefix of the custom controls is &#8220;ass:&#8221;.</p>
<p>In the master page&#8217;s header section, add:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ass:StylePlaceHolder</span> <span style="color: #000066;">ID</span>=<span style="color: #ff0000;">&quot;StylePlaceHolder1&quot;</span> <span style="color: #000066;">runat</span>=<span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000066;">SkinID</span>=<span style="color: #ff0000;">&quot;ThemeStyles&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>In your theme directory, add a skin file (e.g. Styles.skin) and add the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ass:StylePlaceHolder</span> <span style="color: #000066;">runat</span>=<span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000066;">SkinId</span>=<span style="color: #ff0000;">&quot;ThemeStyles&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ass:Style</span> <span style="color: #000066;">CssUrl</span>=<span style="color: #ff0000;">&quot;~/App_Themes/Default/01reset.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ass:Style</span> <span style="color: #000066;">CssUrl</span>=<span style="color: #ff0000;">&quot;~/App_Themes/Default/02style.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ass:Style</span> <span style="color: #000066;">CssUrl</span>=<span style="color: #ff0000;">&quot;~/App_Themes/Default/99iefix.css&quot;</span> <span style="color: #000066;">ConditionCommentExpression</span>=<span style="color: #ff0000;">&quot;[if IE]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ass:StylePlaceHolder<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That is basically it. There are a more properties on the Style control that can be used to control the rendering, but this is the basic setup. With that in place, you can easily add another theme and replace all the styles, since you only need to include a different skin file.</p>
<p>The following is the code for the Style control:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Security.Permissions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>assembly<span style="color: #008000;">:</span> TagPrefix<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Assimilated.Extensions.Web.Controls&quot;</span>, <span style="color: #666666;">&quot;ass&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">namespace</span> Assimilated.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">Stylesheet</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>AspNetHostingPermission<span style="color: #000000;">&#40;</span>SecurityAction.<span style="color: #0000FF;">Demand</span>, Level <span style="color: #008000;">=</span> AspNetHostingPermissionLevel.<span style="color: #0000FF;">Minimal</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>AspNetHostingPermission<span style="color: #000000;">&#40;</span>SecurityAction.<span style="color: #0000FF;">InheritanceDemand</span>, Level <span style="color: #008000;">=</span> AspNetHostingPermissionLevel.<span style="color: #0000FF;">Minimal</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>DefaultProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;CssUrl&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ParseChildren<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;InlineStyle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>PersistChildren<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ToolboxData<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}:Style runat=<span style="color: #008080; font-weight: bold;">\&quot;</span>server<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&lt;/{0}:Style&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>Themeable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Style <span style="color: #008000;">:</span> Control
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> Style<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// set default value... for some reason the DefaultValue attribute do</span>
            <span style="color: #008080; font-style: italic;">// not set this as I would have expected.</span>
            TargetMedia <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;All&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region Properties</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Style sheet&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Description<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;The url to the style sheet.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>UrlProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;*.css&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> CssUrl
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Style sheet&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;All&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Description<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;The target media(s) of the style sheet. See http://www.w3.org/TR/REC-CSS2/media.html for more information.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> TargetMedia
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Style sheet&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span>EmbedType.<span style="color: #0000FF;">Link</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Description<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Specify how to embed the style sheet on the page.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> EmbedType Type
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>PersistenceMode<span style="color: #000000;">&#40;</span>PersistenceMode.<span style="color: #0000FF;">InnerDefaultProperty</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> InlineStyle
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Conditional comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Description<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Specifies a conditional comment expression to wrap the style sheet in. See http://msdn.microsoft.com/en-us/library/ms537512.aspx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ConditionalCommentExpression
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Conditional comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span>CommentType.<span style="color: #0000FF;">DownlevelHidden</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Description<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Whether to reveal the conditional comment expression to downlevel browsers. Default is to hide. See http://msdn.microsoft.com/en-us/library/ms537512.aspx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> CommentType ConditionalCommentType
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Behavior&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> SkinID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Render<span style="color: #000000;">&#40;</span>HtmlTextWriter writer<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>            
            <span style="color: #008080; font-style: italic;">// add empty line to make output pretty</span>
            writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// prints out begin condition comment tag</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>ConditionalCommentExpression<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>ConditionalCommentType <span style="color: #008000;">==</span> CommentType.<span style="color: #0000FF;">DownlevelRevealed</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&lt;!{0}&gt;&quot;</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;&lt;!--{0}&gt;&quot;</span>,
                                 ConditionalCommentExpression<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>CssUrl<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>               
                <span style="color: #008080; font-style: italic;">// add shared attribute</span>
                writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Type</span>, <span style="color: #666666;">&quot;text/css&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// render either import or link tag</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Type <span style="color: #008000;">==</span> EmbedType.<span style="color: #0000FF;">Link</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// &lt;link href=\&quot;{0}\&quot; type=\&quot;text/css\&quot; rel=\&quot;stylesheet\&quot; media=\&quot;{1}\&quot; /&gt;</span>
                    writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Href</span>, ResolveUrl<span style="color: #000000;">&#40;</span>CssUrl<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Rel</span>, <span style="color: #666666;">&quot;stylesheet&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;media&quot;</span>, TargetMedia<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Link</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// &lt;style type=&quot;text/css&quot;&gt;@import &quot;modern.css&quot; screen;&lt;/style&gt;</span>
                    writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Style</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@import <span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> {1};&quot;</span>, ResolveUrl<span style="color: #000000;">&#40;</span>CssUrl<span style="color: #000000;">&#41;</span>, TargetMedia<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>InlineStyle<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// &lt;style type=&quot;text/css&quot;&gt;... inline style ... &lt;/style&gt;</span>
                writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Type</span>, <span style="color: #666666;">&quot;text/css&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Style</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                writer.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>InlineStyle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// prints out end condition comment tag</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>ConditionalCommentExpression<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// add empty line to make output pretty</span>
                writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>ConditionalCommentType <span style="color: #008000;">==</span> CommentType.<span style="color: #0000FF;">DownlevelRevealed</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&lt;![endif]&gt;&quot;</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;&lt;![endif]--&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> EmbedType
    <span style="color: #000000;">&#123;</span>        
        Link <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
        Import <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> CommentType
    <span style="color: #000000;">&#123;</span>
        DownlevelHidden <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
        DownlevelRevealed <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And the code for the StylePlaceHolder control:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Security.Permissions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI.HtmlControls</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>assembly<span style="color: #008000;">:</span> TagPrefix<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Assimilated.Extensions.Web.Controls&quot;</span>, <span style="color: #666666;">&quot;ass&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">namespace</span> Assimilated.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">Stylesheet</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>AspNetHostingPermission<span style="color: #000000;">&#40;</span>SecurityAction.<span style="color: #0000FF;">Demand</span>, Level <span style="color: #008000;">=</span> AspNetHostingPermissionLevel.<span style="color: #0000FF;">Minimal</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>AspNetHostingPermission<span style="color: #000000;">&#40;</span>SecurityAction.<span style="color: #0000FF;">InheritanceDemand</span>, Level <span style="color: #008000;">=</span> AspNetHostingPermissionLevel.<span style="color: #0000FF;">Minimal</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>DefaultProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;SkinID&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ToolboxData<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}:StylePlaceHolder runat=<span style="color: #008080; font-weight: bold;">\&quot;</span>server<span style="color: #008080; font-weight: bold;">\&quot;</span> SkinID=<span style="color: #008080; font-weight: bold;">\&quot;</span>ThemeStyles<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&lt;/{0}:StylePlaceHolder&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ParseChildren<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;Styles&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>Themeable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>PersistChildren<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> StylePlaceHolder <span style="color: #008000;">:</span> Control
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>Style<span style="color: #008000;">&gt;</span> _styles<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Behavior&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DefaultValue<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ThemeStyles&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> SkinID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Browsable<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>Style<span style="color: #008000;">&gt;</span> Styles
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_styles <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    _styles <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Style<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> _styles<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_styles <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                return<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// add child controls</span>
            Styles.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnLoad</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// get notified when page has finished its load stage</span>
            Page.<span style="color: #0000FF;">LoadComplete</span> <span style="color: #008000;">+=</span> Page_LoadComplete<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">void</span> Page_LoadComplete<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// only remove if the page is actually using themes</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">StyleSheetTheme</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">Theme</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Make sure only to remove style sheets from the added by</span>
                <span style="color: #008080; font-style: italic;">// the runtime form the current theme.</span>
                var themePath <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/App_Themes/{0}&quot;</span>,
                                              <span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">StyleSheetTheme</span><span style="color: #000000;">&#41;</span>
                                                  <span style="color: #008000;">?</span> Page.<span style="color: #0000FF;">StyleSheetTheme</span>
                                                  <span style="color: #008000;">:</span> Page.<span style="color: #0000FF;">Theme</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// find all existing stylesheets in header</span>
                var removeCandidate <span style="color: #008000;">=</span> Page.<span style="color: #0000FF;">Header</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>HtmlLink<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                    .<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>link <span style="color: #008000;">=&gt;</span> link.<span style="color: #0000FF;">Href</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>themePath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// remove the automatically added style sheets</span>
                removeCandidate.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">Header</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> AddParsedSubObject<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> obj<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// only add Style controls</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">is</span> Style<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">AddParsedSubObject</span><span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So what do you guys think? Is this a good solution to the asp.net theme problem? And what about the code? These two controls are my first attempt at writing custom server controls, so any input is very much welcome.</p>
<p>Get the code, <a href='http://egilhansen.com/wp-content/assimilatedwebcontrols.zip'>including Visual Studio 2008 solution and .dll</a> if you just want to use the controls.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2008/11/30/how-to-take-control-of-style-sheets-in-aspnet-themes-with-the-styleplaceholder-and-style-control/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Simply way of adding a default item to a data bound DropDownList</title>
		<link>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/</link>
		<comments>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 09:55:53 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[DropDownList]]></category>

		<guid isPermaLink="false">http://www.egil.dk/?p=81</guid>
		<description><![CDATA[I have always found it too difficult to add a &#8220;Choose xxxxx&#8221; item to the top of a DropDownList, when the DropDownList binds to a ObjectDataSource, SqlDataSource or similar. I almost always ended up just feeding the DropDownList its content from the code behind, even though it felt like something that should be possible directly. [...]]]></description>
			<content:encoded><![CDATA[<p>I have always found it too difficult to add a &#8220;Choose xxxxx&#8221; item to the top of a <code>DropDownList</code>, when the <code>DropDownList </code>binds to a <code>ObjectDataSource</code>, <code>SqlDataSource </code>or similar. I almost always ended up just feeding the <code>DropDownList </code>its content from the code behind, even though it felt like something that should be possible directly. The control do have a <code>AppendDataBoundItems </code>property, but most of the time you just end up with duplicated items.</p>
<p>Today I discovered the simplest and most elegant solution I&#8217;ve seen to date. One line of code in the code behind file is still required, but the elegance of DataSource controls is kept. In short, once the <code>DropDownList </code>has bound to its data source, add the &#8220;Choose xxxxx&#8221; item to the items collection of the DropDownList. The trick is to use the <code>Insert </code>method instead of the <code>Add </code>method, since the Insert method allows you to specify where the new item should be inserted, and not just added to the end of the item list.</p>
<p>In this example I use the <code>OnDataBound </code>event to insert the new item:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;ddlCustomer&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> 
    DataSourceID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;odsCustomer&quot;</span> DataTextField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;DisplayName&quot;</span> 
    DataValueField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;CustomerId&quot;</span> OnDataBound<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;ddlCustomer_DataBound&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList<span style="color: #006600; font-weight: bold;">&gt;</span></pre></div></div>

<p>The code behind is just one line of code (excluding the method definition):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> ddlCustomer_DataBound<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ddlCustomer.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #008000;">new</span> ListItem<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Choose a Customer&quot;</span>, <span style="color: #666666;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>System.Diagnostics + ASP.Net Web Site &#8211; remember to set compilerOptions=&#8221;/d:TRACE&#8221;</title>
		<link>http://egilhansen.com/2008/06/03/systemdiagnostics-aspnet-website-remember-to-set-compileroptions-d-trace/</link>
		<comments>http://egilhansen.com/2008/06/03/systemdiagnostics-aspnet-website-remember-to-set-compileroptions-d-trace/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 21:18:42 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[System.Diagnostics]]></category>
		<category><![CDATA[Ukadc.Diagnostics]]></category>

		<guid isPermaLink="false">http://www.egil.dk/?p=80</guid>
		<description><![CDATA[Today I was looking at Ukadc.Diagnostics, an extension to the System.Diagnostics namespace, and decided to build a test website myself to get more familiar with it. After reading through Ukadc.Diagnostics Reference Example I thought it would be a homerun in the first try, but it turns out that without adding compilerOptions="/d:TRACE" to each compiler in [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was looking at <a href="http://www.codeplex.com/UkadcDiagnostics">Ukadc.Diagnostics</a>, an extension to the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.aspx">System.Diagnostics namespace</a>, and decided to build a test website myself to get more familiar with it. After reading through <a href="http://www.codeplex.com/UkadcDiagnostics/Wiki/View.aspx?title=QuickReferenceExample&amp;referringTitle=Home">Ukadc.Diagnostics Reference Example</a> I thought it would be a homerun in the first try, but it turns out that without adding <code>compilerOptions="/d:TRACE"</code> to each compiler in the compilers section in the Web.config, the tracing code is never compiled into the assemblies at runtime. Needless to say, I was scratching my head since my sample console application worked as expected the first time.</p>
<p>My compilers section ended up looking like this (it is located in the system.codedom section)</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compilers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compiler</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;c#;cs;csharp&quot;</span> <span style="color: #000066;">extension</span>=<span style="color: #ff0000;">&quot;.cs&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">compilerOptions</span>=<span style="color: #ff0000;">&quot;/d:TRACE&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">warningLevel</span>=<span style="color: #ff0000;">&quot;4&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providerOption</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CompilerVersion&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;v3.5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providerOption</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WarnAsError&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compiler<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compiler</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;vb;vbs;visualbasic;vbscript&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">compilerOptions</span>=<span style="color: #ff0000;">&quot;/d:Trace=true&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">extension</span>=<span style="color: #ff0000;">&quot;.vb&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">warningLevel</span>=<span style="color: #ff0000;">&quot;4&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providerOption</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CompilerVersion&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;v3.5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providerOption</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;OptionInfer&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providerOption</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WarnAsError&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compiler<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compilers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Do note that this is not an issue with ASP.Net Web Applications, since they are precompiled. For more on the subject, <a href="http://msdn.microsoft.com/en-us/library/b0ectfxd.aspx">MSDN has a great article</a> which certainly saved me in this instance.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2008/06/03/systemdiagnostics-aspnet-website-remember-to-set-compileroptions-d-trace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safe way to convert UTC time to local time when databinding</title>
		<link>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/</link>
		<comments>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 17:43:18 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.egil.dk/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/</guid>
		<description><![CDATA[After reading Scott Mitchell&#8217;s article Using Coordinated Universal Time (UTC) to Store Date/Time Values, I decided to do as he suggest and use UTC for a web application I&#8217;m building at work. All in all it is fairly easy to deal with UTC in the .NET Framwork. There is a DateTime.UtcNow you can use instead [...]]]></description>
			<content:encoded><![CDATA[<p>After reading Scott Mitchell&#8217;s article <a href="http://aspnet.4guysfromrolla.com/articles/081507-1.aspx">Using Coordinated Universal Time (UTC) to Store Date/Time Values</a>, I decided to do as he suggest and use UTC for a web application I&#8217;m building at work. All in all it is fairly easy to deal with UTC in the .NET Framwork. There is a <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.utcnow.aspx">DateTime.UtcNow</a> you can use instead of the regular DateTime.Now property, and there is a <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx">DateTime.ToLocalTime</a> method that will convert your UTC DateTime value to the local server time zone, even taking daylight savings time in to consideration. When converting the other way, the <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx">DateTime.ToUniversalTime</a> method is provided.</p>
<p>With MsSQL&#8217;s getutcdate() function (instead of getdate()), just about everything is smooth sailing. The only problem I have encountered is when binding a to a ASP.NET server control. Scott suggests doing like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span> Eval<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DateUpdated&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>This only works when DateUpdated is not null, which is not always the case in my application. To get around this, I created the following simple class and stuck it in my App_Code folder.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Extensions
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If object is a DateTime, it will convert</span>
    <span style="color: #008080; font-style: italic;">/// the DateTime to local time.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;obj&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> ToLocalTime<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">object</span> obj<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">is</span> DateTime<span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span>obj<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">else</span>
            <span style="color: #0600FF;">return</span> obj<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The method <span style="font-family: Courier New">ToLocalTime(this object obj)</span>uses the extension method feature in .NET 3.0, that allows you to extend the functionality of other class. Here I extend upon Object, adding a .ToLocalTime() method to it. This allows me to use it in ASP.NET server controls like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Eval<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DateUpdated&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>This works because Eval() returns a object, which is also the reason why I did not extend a more specific class.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updating a control in a MasterPage from a UserControl</title>
		<link>http://egilhansen.com/2007/07/05/updating-a-control-in-a-masterpage-from-a-usercontrol/</link>
		<comments>http://egilhansen.com/2007/07/05/updating-a-control-in-a-masterpage-from-a-usercontrol/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 17:16:09 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.egil.dk/2007/07/05/updating-a-control-in-a-masterpage-from-a-usercontrol/</guid>
		<description><![CDATA[Problem: You have a control, for example a Label, in a MasterPage that you want to update from a UserControl added to a page that resides in the MasterPage&#8217;s ContentPlaceHolder control (see figure 1). Figure 1: From MasterPage to UserControl I was facing this problem a few months back on a project, where I had [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong> You have a control, for example a Label, in a MasterPage that you want to update from a UserControl added to a page that resides in the MasterPage&#8217;s ContentPlaceHolder control (see figure 1).</p>
<p><img src="/wp-content/070507-1718-updatingaco119.png" /></p>
<p><span style="color: #4f81bd; font-size: 9pt"><strong>Figure 1: From MasterPage to UserControl<br />
</strong></span></p>
<p>I was facing this problem a few months back on a project, where I had many different UserControls that was added to a limited number of pages, and all the pages had the same master page. I wanted an elegant way of updating the status label in my MasterPage while limiting the amount of redundant code. After googling around a bit, I ended up combining a few different solutions into the following.</p>
<p><span id="more-31"></span><strong>Solution:</strong> The idea is to use the event system in .NET and give each UserControl an event that the UserControl can fire whenever it wants to update the status label. Pages that host the UserControl can then subscribe to the event and update the status label on the MasterPage when the event fires.</p>
<p><strong>Here&#8217;s an overview of the solution<br />
</strong></p>
<ol>
<li>Declare a new event in the UserControl.</li>
<li>Declare a new event argument.</li>
<li>On the ASPX page, subscribe to the UserControls event and make an event handler that updates the status label on the master page.</li>
<li>Create a public property on the MasterPage for each access to the status label.</li>
</ol>
<p><strong>1: Declare a new event in the UserControl<br />
</strong>To avoid declaring the event each time I added a new UserControl to my project, I created a new class UserControlBase, which extends System.Web.UI.UserControl, and made all UserControls extend this class, instead of the default System.Web.UI.UserControl. This way I could put all the redundant UserControl code in one file, and be done with it.</p>
<pre><code>// Declare the event handler

public event EventHandler&lt;UpdateStatusEventArgs&gt; StatusLabelUpdateEvent;

</code></pre>
<pre><code>/// &lt;summary&gt;

/// Method for triggering the UpdateStatusEventArgs.

/// &lt;/summary&gt;

/// &lt;param name="message"&gt;Status message to pass to the subscribers&lt;/param&gt;

protected void UpdateStatusLabel(string message)

{

    // Copy to a temporary variable to be thread-safe.

    EventHandler&lt;UpdateStatusEventArgs&gt; temp = StatusLabelUpdateEvent;

</code></pre>
<pre><code>    if (temp != null)

        temp(this, new UpdateStatusEventArgs(message));

}

</code></pre>
<p>And here is the code for the UserControl (notice the new : UserControlBase in the class declaration):</p>
<pre><code>public partial class UserControls_UpdateStatusUserControl : UserControlBase

{

    protected void Page_Load(object sender, EventArgs e) { }

</code></pre>
<pre><code>    protected void submitButton_Click(object sender, EventArgs e)

    {

        base.UpdateStatusLabel(messageTextBox.Text);

    }

}

</code></pre>
<p><strong>2: Declare a new event argument<br />
</strong>I also needed to create an new class â€“ UpdateStatusEventArgs, which extends System.EventArgs, so the UserControls would be able to pass an message back to the ASPX page that can then update the status label.</p>
<pre><code>/// &lt;summary&gt;

/// A status label event args class.

/// &lt;/summary&gt;

public class UpdateStatusEventArgs : EventArgs

{

</code></pre>
<pre><code>    // private fields

    private string _message;

</code></pre>
<pre><code>    /// &lt;summary&gt;

    /// Create an Status Label Event Args with a string

    /// message and default type is StatusType.OK.

    /// &lt;/summary&gt;

    public UpdateStatusEventArgs() { }

</code></pre>
<pre><code>    /// &lt;summary&gt;

    /// Create an Status Label Event Args with a

    /// string message and default type is StatusType.OK.

</code></pre>
<pre><code>    /// &lt;/summary&gt;

    /// &lt;param name="message"&gt;Update message&lt;/param&gt;

    public UpdateStatusEventArgs(string message)

    {

        _message = message;

    }

</code></pre>
<pre><code>    /// &lt;summary&gt;

    /// Get/set status message

    /// &lt;/summary&gt;

    public string Message

    {

</code></pre>
<pre><code>        get { return _message; }

        set { _message = value; }

    }

}

</code></pre>
<p><strong>3: On the ASPX page, subscribe to the UserControls event.<br />
</strong>On my ASPX page I do two simple things. First I declare my master page through the <a href="http://msdn2.microsoft.com/en-us/library/ms228274.aspx">MasterPage directive</a>, just below Page directive, like so:</p>
<p>&lt;%@ MasterType VirtualPath=&#8221;~/MasterPage.master&#8221; %&gt;</p>
<p>This provides me with a strongly typed reference to my master page, which makes it a breeze to access properties and methods on it.</p>
<p>In the code behind file I subscribe to the user controls update event, and in my event handler update the status label on the master page.</p>
<pre><code>protected void Page_Load(object sender, EventArgs e)

{

    LoadUserControl();

}

</code></pre>
<pre><code>/// &lt;summary&gt;

/// Loads user control in to the ContentPlaceHolder.

/// &lt;/summary&gt;

private void LoadUserControl()

{

    // Declare usercontrol

    UserControlBase userControl;

</code></pre>
<pre><code>    // Load correct usercontrol

    userControl =

    Page.LoadControl("~/UserControls/UpdateStatusUserControl.ascx")

    as UserControlBase;

    // Subscribe to StatusLabelUpdateEvent

    userControl.StatusLabelUpdateEvent +=

    new EventHandler&lt;UpdateStatusEventArgs&gt;(UpdateStatusLabelEvent);

</code></pre>
<pre><code>    // Add it to the page

    PlaceHolder.Controls.Add(userControl);

}

</code></pre>
<pre><code>// Event handler, used when a status label

// update request is being triggered

protected void UpdateStatusLabelEvent(object sender, UpdateStatusEventArgs e)

{

    Master.StatusLabel.Text = e.Message;

}

</code></pre>
<p><strong>4: Create a public property on the MasterPage for each access to the status label.<br />
</strong>The last thing I needed to make was a public property on the master page that returns a reference to the status label.</p>
<pre><code>public Label StatusLabel

{

    get { return statusLabel; }

}
</code></pre>
<p>That&#8217;s it!</p>
<p>Please excuse the horrible source code in this blog post, I haven&#8217;t found a good plugin to do this probably for me yet. I have created a <a href="/code/UpdatingControlOnMasterPageFromUserControl.zip">Visual Studio example project</a>, it might be easier to read the code if you just download that and view the source code from Visual Studio.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2007/07/05/updating-a-control-in-a-masterpage-from-a-usercontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
