<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>William Duffy</title>
	
	<link>http://www.wduffy.co.uk/blog</link>
	<description>Glasgow Based ASP.NET Web Developer</description>
	<pubDate>Mon, 18 Jan 2010 09:00:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/wduffy" /><feedburner:info uri="wduffy" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Order by day column using SQL</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/hAe_7SVSFJw/</link>
		<comments>http://www.wduffy.co.uk/blog/order-by-day-column-sql/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 09:00:02 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[CASE]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[Transact-SQL]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=536</guid>
		<description><![CDATA[If you&#8217;ve ever had to sort a SQL query&#8217;s resultset using an &#8220;order by&#8221; clause on a &#8220;day of the week&#8221; column then you will know it can be very easy, or very tricky, depending on the format that the day is stored in the database.
If you stored the day of the week in numerical [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span>f you&#8217;ve ever had to sort a SQL query&#8217;s resultset using an &#8220;order by&#8221; clause on a &#8220;day of the week&#8221; column then you will know it can be very easy, or very tricky, depending on the format that the day is stored in the database.</p>
<p>If you stored the day of the week in numerical format (1 = Sunday, 7 = Saturday) then sorting can be accomplished via a simple order by clause.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SELECT
     <span style="color: #f7f4ef;">*</span>
FROM
     classes
ORDER BY
     day ASC</pre></td></tr></table></div>

<p>However, if you have saved the day of the week in textual format then things become a little trickier.</p>
<p>The problem is &#8220;order by&#8221; on a textual value sorts alphanumerically from a-z and has no comprehension about the order of weekdays. In this situation Thursday comes before Wednesday, Saturday before Tuesday, etc. To get around this we need to let the SQL query know what order to handle the weekdays in. </p>
<p>Transact-SQL&#8217;s CASE expression is perfect for this task. All we have to do is assess the day string in the &#8220;order by&#8221; clause and return a numerical value depending on the result of the expression.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SELECT
     <span style="color: #f7f4ef;">*</span>
FROM
     classes
ORDER BY 
     <span style="color: #87BCD2;">CASE</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Sunday'</span> THEN <span style="color: #D6618C;">1</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Monday'</span> THEN <span style="color: #D6618C;">2</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Tuesday'</span> THEN <span style="color: #D6618C;">3</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Wednesday'</span> THEN <span style="color: #D6618C;">4</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Thursday'</span> THEN <span style="color: #D6618C;">5</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Friday'</span> THEN <span style="color: #D6618C;">6</span>
          WHEN Day <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'Saturday'</span> THEN <span style="color: #D6618C;">7</span>
     END ASC</pre></td></tr></table></div>

<p>The resultset will now be sorted based on the textual &#8220;day of the week&#8221; field, exactly as it would be if the field held a numeric value. </p>
<p>For more details on Transact-SQL&#8217;s CASE expression see <a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx">http://msdn.microsoft.com/en-us/library/ms181765.aspx</a>.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/hAe_7SVSFJw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/order-by-day-column-sql/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/order-by-day-column-sql/</feedburner:origLink></item>
		<item>
		<title>MacBook Pro hash key in windows</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/U6U3EpduiE8/</link>
		<comments>http://www.wduffy.co.uk/blog/macbook-pro-hash-key-in-windows/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 22:49:12 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[Ranting]]></category>

		<category><![CDATA[Apple]]></category>

		<category><![CDATA[MacBook Pro]]></category>

		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=554</guid>
		<description><![CDATA[Can&#8217;t find the # hash key # on the keyboard of your MacBook Pro running Windows? I spent ages scratching my head on this one but it is there, it&#8217;s just hidden away from sight.
Simply hold ctrl, alt and press 3 to get a nice little hash character onto your screen. Simples!
]]></description>
			<content:encoded><![CDATA[<p><span class="drop">C</span>an&#8217;t find the # hash key # on the keyboard of your MacBook Pro running Windows? I spent ages scratching my head on this one but it is there, it&#8217;s just hidden away from sight.</p>
<p>Simply hold ctrl, alt and press 3 to get a nice little hash character onto your screen. Simples!</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/U6U3EpduiE8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/macbook-pro-hash-key-in-windows/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/macbook-pro-hash-key-in-windows/</feedburner:origLink></item>
		<item>
		<title>C# Literal Types</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/9yF57USPZsE/</link>
		<comments>http://www.wduffy.co.uk/blog/c-literal-types/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 20:23:36 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[literal types]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=422</guid>
		<description><![CDATA[When you are specifying literal numeric values in your code you will, on occasion, have to give the compiler a heads-up on the expected type from the declaration. 
For example, the literal value 5.3 will, by default, be assumed to be a double. As such, the following

1
float result = 5.3 / 12.4;

will result in the [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">W</span>hen you are specifying literal numeric values in your code you will, on occasion, have to give the compiler a heads-up on the expected type from the declaration. </p>
<p>For example, the literal value 5.3 will, by default, be assumed to be a double. As such, the following</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">float</span> result <span style="color: #f7f4ef;">=</span> <span style="color: #D6618C;">5.3</span> <span style="color: #f7f4ef;">/</span> <span style="color: #D6618C;">12.4</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>will result in the exception </p>
<h3>Cannot implicitly convert type &#8216;double&#8217; to &#8216;float&#8217;. An explicit conversion exists (are you missing a cast?)</h3>
<p>You could cast the value but that would create an unneccessary overhead and result in messy code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">float</span> result <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">float</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #D6618C;">5.3</span> <span style="color: #f7f4ef;">/</span> <span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">float</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #D6618C;">12.4</span><span style="color: #f7f4ef;">;</span> 
or
<span style="color: #D6618C;">float</span> result <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">float</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">5.3</span> <span style="color: #f7f4ef;">/</span> <span style="color: #D6618C;">12.4</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>The most suitable way to approach this is to advise the compiler on the literal type. You do this by applying a suffix to your literal.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">float</span> result <span style="color: #f7f4ef;">=</span> 5.3f <span style="color: #f7f4ef;">/</span> 12.4f<span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>This can also be used when passing a literal as a parameter in a method signature.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">MyObject.<span style="color: #87bcd2;">MyMethod</span><span style="color: #f7f4ef;">&#40;</span>12.5m<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>A table of the most common literal type declarations can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1m<span style="color: #f7f4ef;">;</span>  <span style="color: #008080; font-style: italic;">// decimal</span>
<span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1f<span style="color: #f7f4ef;">;</span>  <span style="color: #008080; font-style: italic;">// float</span>
<span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1d<span style="color: #f7f4ef;">;</span>  <span style="color: #008080; font-style: italic;">// double</span>
<span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1l<span style="color: #f7f4ef;">;</span>  <span style="color: #008080; font-style: italic;">// long</span>
<span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1u<span style="color: #f7f4ef;">;</span>  <span style="color: #008080; font-style: italic;">// uint</span>
<span style="color: #D6618C;">var</span> x <span style="color: #f7f4ef;">=</span> 1ul<span style="color: #f7f4ef;">;</span> <span style="color: #008080; font-style: italic;">// ulong</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/9yF57USPZsE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/c-literal-types/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/c-literal-types/</feedburner:origLink></item>
		<item>
		<title>400 Bad request in Firefox, fine in IE</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/RpIJVR3GLK8/</link>
		<comments>http://www.wduffy.co.uk/blog/400-bad-request-in-firefox-fine-in-ie/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 09:25:19 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[Ranting]]></category>

		<category><![CDATA[400 Bad Request]]></category>

		<category><![CDATA[Cookies]]></category>

		<category><![CDATA[Firefox]]></category>

		<category><![CDATA[IE]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=529</guid>
		<description><![CDATA[I ran into a problem this morning that had me stumped. One of our sites was running fine in IE but in Firefox was causing a &#8220;400 Bad Request&#8221;. To make matters more confusing the server was hosting two domains, one of them working in Firefox and one not. After assessing the server and ensuring [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span> ran into a problem this morning that had me stumped. One of our sites was running fine in IE but in Firefox was causing a &#8220;400 Bad Request&#8221;. To make matters more confusing the server was hosting two domains, one of them working in Firefox and one not. After assessing the server and ensuring everything was set-up properly I knew the problem had to be related to the browser. Sure enough, after explicitly removing all cookies from Firefox the problem was solved. Weird :S</p>
<p>Tools > Options > Privacy > Show Cookies > Remove All Cookies</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/RpIJVR3GLK8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/400-bad-request-in-firefox-fine-in-ie/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/400-bad-request-in-firefox-fine-in-ie/</feedburner:origLink></item>
		<item>
		<title>RssResult - An ASP.NET MVC RSS ActionResult</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/TsR-uF6PGio/</link>
		<comments>http://www.wduffy.co.uk/blog/rssresult-aspnet-mvc-rss-actionresult/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 09:55:59 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[MVC]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[ActionResult]]></category>

		<category><![CDATA[Dependency Injection]]></category>

		<category><![CDATA[RSS]]></category>

		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=455</guid>
		<description><![CDATA[One of the most common tasks for data serving websites is offering an RSS feed that users can subscribe to in order to keep up with the latest updates to that data.
Previously I posted the article Creating a simple RSS feed using ASP.NET MVC which demonstrated how to create an RSS feed in a quick, [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">O</span>ne of the most common tasks for data serving websites is offering an RSS feed that users can subscribe to in order to keep up with the latest updates to that data.</p>
<p>Previously I posted the article <a href="http://www.wduffy.co.uk/blog/creating-a-simple-rss-feed-using-aspnet-mvc/">Creating a simple RSS feed using ASP.NET MVC</a> which demonstrated how to create an RSS feed in a quick, easy way. Here, I will show you how to create an RssResult to be returned directly from your controller. This offers a much more scalable, reusable approach that is flexible enough to handle any data that you wish to serve and is designed specifically for addition to your ASP.NET MVC stack.</p>
<p>The RssResult must be capable of taking any data and knowing how to render an RSS item from it (i.e. news, events, posts, comments, reviews etc). So before we create the RssResult we need to abstract the structure of an RSS item. We do this by creating an interface abstraction to accommodate this. Any object that is to be served can implement this interface allowing the RssResult to work with it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System</span>
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">interface</span> IRss
    <span style="color: #f7f4ef;">&#123;</span>
        <span style="color: #D6618C;">string</span> Title <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
        <span style="color: #D6618C;">string</span> Description <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
        <span style="color: #D6618C;">string</span> Link <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>The standard return type of a controller&#8217;s methods is the abstract type ActionResult. In most cases the actual returned object will be of type ViewResult, which will look for a view to use when rendering your model to the response stream. Here, our RssResult will inherit from ActionResult, just as ViewResult does, but will internally render its own XML output and alter the response&#8217;s content-type header to xml, avoiding any view files or extra work in the controller. Using dependency injection the RssResult will not be instantiable without being supplied with the data it has to serve, which will be a List<IRss>. It will also accept a title and description for the feed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">using</span> <span style="color: #008080;">System</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #f7f4ef;">;</span>
&nbsp;
<span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System.<span style="color: #87bcd2;">Web</span></span>.<span style="color: #87bcd2;">Mvc</span>
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> RssResult <span style="color: #f7f4ef;">:</span> ActionResult
    <span style="color: #f7f4ef;">&#123;</span>
&nbsp;
        <span style="color: #87BCD2;">private</span> List<span style="color: #f7f4ef;">&lt;</span>IRss<span style="color: #f7f4ef;">&gt;</span> _items<span style="color: #f7f4ef;">;</span>
        <span style="color: #87BCD2;">private</span> <span style="color: #D6618C;">string</span> _title<span style="color: #f7f4ef;">;</span>
        <span style="color: #87BCD2;">private</span> <span style="color: #D6618C;">string</span> _description<span style="color: #f7f4ef;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Initialises the RssResult</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;items&quot;&gt;The items to be added to the rss feed.&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;title&quot;&gt;The title of the rss feed.&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;description&quot;&gt;A short description about the rss feed.&lt;/param&gt;</span>
        <span style="color: #87BCD2;">public</span> RssResult<span style="color: #f7f4ef;">&#40;</span>IEnumerable<span style="color: #f7f4ef;">&lt;</span>IRss<span style="color: #f7f4ef;">&gt;</span> items, <span style="color: #D6618C;">string</span> title, <span style="color: #D6618C;">string</span> description<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            _items <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> List<span style="color: #f7f4ef;">&lt;</span>IRss<span style="color: #f7f4ef;">&gt;</span><span style="color: #f7f4ef;">&#40;</span>items<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
            _title <span style="color: #f7f4ef;">=</span> title<span style="color: #f7f4ef;">;</span>
            _description <span style="color: #f7f4ef;">=</span> description<span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
        <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">override</span> <span style="color: #87BCD2;">void</span> ExecuteResult<span style="color: #f7f4ef;">&#40;</span>ControllerContext context<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            XmlWriterSettings settings <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> XmlWriterSettings<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
            settings.<span style="color: #87bcd2;">Indent</span> <span style="color: #f7f4ef;">=</span> true<span style="color: #f7f4ef;">;</span>
            settings.<span style="color: #87bcd2;">NewLineHandling</span> <span style="color: #f7f4ef;">=</span> NewLineHandling.<span style="color: #87bcd2;">Entitize</span><span style="color: #f7f4ef;">;</span>
&nbsp;
            context.<span style="color: #87bcd2;">HttpContext</span>.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">ContentType</span> <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;text/xml&quot;</span><span style="color: #f7f4ef;">;</span>
            <span style="color: #87BCD2;">using</span> <span style="color: #f7f4ef;">&#40;</span>XmlWriter _writer <span style="color: #f7f4ef;">=</span> XmlWriter.<span style="color: #87bcd2;">Create</span><span style="color: #f7f4ef;">&#40;</span>context.<span style="color: #87bcd2;">HttpContext</span>.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">OutputStream</span>, settings<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span>
            <span style="color: #f7f4ef;">&#123;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Begin structure</span>
                _writer.<span style="color: #87bcd2;">WriteStartElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;rss&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                _writer.<span style="color: #87bcd2;">WriteAttributeString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;version&quot;</span>, <span style="color: #acacac;">&quot;2.0&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                _writer.<span style="color: #87bcd2;">WriteStartElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;channel&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
                _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;title&quot;</span>, _title<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;description&quot;</span>, _description<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;link&quot;</span>, context.<span style="color: #87bcd2;">HttpContext</span>.<span style="color: #87bcd2;">Request</span>.<span style="color: #87bcd2;">Url</span>.<span style="color: #87bcd2;">GetLeftPart</span><span style="color: #f7f4ef;">&#40;</span>UriPartial.<span style="color: #87bcd2;">Authority</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Individual items</span>
                _items.<span style="color: #87BCD2;">ForEach</span><span style="color: #f7f4ef;">&#40;</span>x <span style="color: #f7f4ef;">=&gt;</span>
                <span style="color: #f7f4ef;">&#123;</span>
                    _writer.<span style="color: #87bcd2;">WriteStartElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;item&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                    _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;title&quot;</span>, x.<span style="color: #87bcd2;">Title</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                    _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;description&quot;</span>, x.<span style="color: #87bcd2;">Description</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                    _writer.<span style="color: #87bcd2;">WriteElementString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;link&quot;</span>, context.<span style="color: #87bcd2;">HttpContext</span>.<span style="color: #87bcd2;">Request</span>.<span style="color: #87bcd2;">Url</span>.<span style="color: #87bcd2;">GetLeftPart</span><span style="color: #f7f4ef;">&#40;</span>UriPartial.<span style="color: #87bcd2;">Authority</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">+</span> x.<span style="color: #87bcd2;">Link</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                    _writer.<span style="color: #87bcd2;">WriteEndElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// End structure</span>
                _writer.<span style="color: #87bcd2;">WriteEndElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                _writer.<span style="color: #87bcd2;">WriteEndElement</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
            <span style="color: #f7f4ef;">&#125;</span>            
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
    <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>As you can see above, the RssResult itself is pretty simple. It takes the collection of IRss items in it’s constructor. By inheriting the abstract type ActionResult it must override ExecuteResult which is called by the framework when it is returned from the controller, which you can see an example of below. One thing to notice in the following code snippet is that the original list of items must be cast to a list of IRss. This is due to the limitations of covariance and contravariance on generics in the C# language. Future versions may support this so that casting will not be required.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">public</span> ActionResult Feed<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            IEnumerable<span style="color: #f7f4ef;">&lt;</span>IRss<span style="color: #f7f4ef;">&gt;</span> news <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> NewsService<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">GetByLatest</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">Cast</span><span style="color: #f7f4ef;">&lt;</span>IRss<span style="color: #f7f4ef;">&gt;</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
            <span style="color: #87BCD2;">return</span> <span style="color: #f7f4ef;">new</span> RssResult<span style="color: #f7f4ef;">&#40;</span>news, <span style="color: #acacac;">&quot;William Duffy - Glasgow Based ASP.NET Web Developer&quot;</span>, <span style="color: #acacac;">&quot;The latest news on ASP.NET, C# and ASP.NET MVC &quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/TsR-uF6PGio" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/rssresult-aspnet-mvc-rss-actionresult/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/rssresult-aspnet-mvc-rss-actionresult/</feedburner:origLink></item>
		<item>
		<title>C# boolean to string extension method</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/KoxXXsqK_GU/</link>
		<comments>http://www.wduffy.co.uk/blog/csharp-boolean-to-string-extension-method/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 10:34:09 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Boolean]]></category>

		<category><![CDATA[Extension Method]]></category>

		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=451</guid>
		<description><![CDATA[One of the most common operations I find myself repeating when programming is writing a boolean value in a human friendly form. Most users, unless geeky and appreciative of the coolness of datatypes, will not be too comfortable with true/false being used in yes/no context. 
I&#8217;m a BIG believer in developing my own reusable code [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">O</span>ne of the most common operations I find myself repeating when programming is writing a boolean value in a human friendly form. Most users, unless geeky and appreciative of the coolness of datatypes, will not be too comfortable with true/false being used in yes/no context. </p>
<p>I&#8217;m a BIG believer in developing my own reusable code libraries and frameworks as I work, so I decided to create a reusable ToString() extension method on the boolean type to cater for this scenario, and thought I&#8217;d share it&#8230;cause that&#8217;s the kind of guy I am!</p>
<p>It&#8217;s important to note that I put the extension method within the System namespace. This allows the extension to be available anywhere in your application. Also, I use a regular expression in the implementation, so be sure to import the RegularExpressions namespace.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #f7f4ef;">;</span>
&nbsp;
<span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System</span>
<span style="color: #f7f4ef;">&#123;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>The first requirement is a simple enum that allows programmatic access to the most common conversions. This lets programmers avoid typing string values if possible, reducing spelling mistakes (which is always a good thing when it comes to us programmers). The enum represents the output in camel case with values in the format TrueFalse.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">///&lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">///Defines a textual boolean value</span>
<span style="color: #008080; font-style: italic;">///&lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">///&lt;remarks&gt;&lt;/remarks&gt;</span>
<span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">enum</span> BooleanText
<span style="color: #f7f4ef;">&#123;</span>
    AcceptedDeclined,
    ActiveInactive,
    CheckedUnchecked,
    CorrectIncorrent,
    EnabledDisabled,
    OnOff,
    YesNo
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>Next we create a static class to expose our extension methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">class</span> BooleanExtensions
<span style="color: #f7f4ef;">&#123;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>The first ToString() extension method accepts a single parameter of type BooleanText. It runs a super simple regular expression to parse the BooleanText value into two seperate true/false values based on the camel case formatting and then makes a call to the second extension method with the results.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Assesses a boolean value and returns a textual value</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;text&quot;&gt;The textual value to be returned&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A textual representation of the boolean value&lt;/returns&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span>
<span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">string</span> ToString<span style="color: #f7f4ef;">&#40;</span><span style="color: #87BCD2;">this</span> <span style="color: #D6618C;">bool</span> value, BooleanText text<span style="color: #f7f4ef;">&#41;</span>
<span style="color: #f7f4ef;">&#123;</span>
    MatchCollection matches <span style="color: #f7f4ef;">=</span> Regex.<span style="color: #87bcd2;">Matches</span><span style="color: #f7f4ef;">&#40;</span>text.<span style="color: #87bcd2;">ToString</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>, <span style="color: #acacac;">&quot;[A-Z][a-z]+&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
    <span style="color: #87BCD2;">return</span> value.<span style="color: #87bcd2;">ToString</span><span style="color: #f7f4ef;">&#40;</span>matches<span style="color: #f7f4ef;">&#91;</span><span style="color: #D6618C;">0</span><span style="color: #f7f4ef;">&#93;</span>.<span style="color: #87bcd2;">Value</span>, matches<span style="color: #f7f4ef;">&#91;</span><span style="color: #D6618C;">1</span><span style="color: #f7f4ef;">&#93;</span>.<span style="color: #87bcd2;">Value</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>The second ToString() extension method accepts two parameters of type System.String. This caters for programmers when the common values in BooleanText are not suitable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Assesses a boolean value and returns a specified word in respect to true or false</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;trueValue&quot;&gt;The textual value to be returned if the current boolean is true&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;falseValue&quot;&gt;The textual value to be returned if the current boolean is false&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A string representation of the current boolean value&lt;/returns&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span>
<span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">string</span> ToString<span style="color: #f7f4ef;">&#40;</span><span style="color: #87BCD2;">this</span> <span style="color: #D6618C;">bool</span> value, <span style="color: #D6618C;">string</span> trueValue, <span style="color: #D6618C;">string</span> falseValue<span style="color: #f7f4ef;">&#41;</span>
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">return</span> <span style="color: #f7f4ef;">&#40;</span>value<span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">?</span> trueValue <span style="color: #f7f4ef;">:</span> falseValue<span style="color: #f7f4ef;">;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s now simply a case of calling ToString() on your boolean value and getting the output you need straight from the type, rather than writing if/else logic into your code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">bool</span> value <span style="color: #f7f4ef;">=</span> true<span style="color: #f7f4ef;">;</span>
lblValue.<span style="color: #87bcd2;">Text</span> <span style="color: #f7f4ef;">=</span> value.<span style="color: #87bcd2;">ToString</span><span style="color: #f7f4ef;">&#40;</span>BooleanWord.<span style="color: #87bcd2;">EnabledDisabled</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/KoxXXsqK_GU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/csharp-boolean-to-string-extension-method/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/csharp-boolean-to-string-extension-method/</feedburner:origLink></item>
		<item>
		<title>CSS class property in ASP.NET MVC HtmlAttributes</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/BW_3-fWsScI/</link>
		<comments>http://www.wduffy.co.uk/blog/css-class-property-asp-net-mvc-htmlattributes/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 15:30:44 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[HTML & CSS]]></category>

		<category><![CDATA[MVC]]></category>

		<category><![CDATA[css]]></category>

		<category><![CDATA[keyword]]></category>

		<category><![CDATA[reserved]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=448</guid>
		<description><![CDATA[When adding an ActionLink or using a similar Html helper on your C# ASP.NET MVC views you will on occasions want to pass a css class via the htmlAttributes parameter. As this is normally achieved by creating an anonymous type you may expect the following code snippet to work.

1
&#60;%= Html.ActionLink&#40;&#34;My Link&#34;, &#34;MyLink&#34;, null, new &#123; [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">W</span>hen adding an ActionLink or using a similar Html helper on your C# ASP.NET MVC views you will on occasions want to pass a css class via the htmlAttributes parameter. As this is normally achieved by creating an anonymous type you may expect the following code snippet to work.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #f7f4ef;">&lt;%=</span> Html.<span style="color: #87bcd2;">ActionLink</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;My Link&quot;</span>, <span style="color: #acacac;">&quot;MyLink&quot;</span>, <span style="color: #87BCD2;">null</span>, <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> <span style="color: #D6618C;">class</span> <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;test&quot;</span> <span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">%&gt;</span></pre></td></tr></table></div>

<p>However, a problem arises with the class property, because class is a reserved word the compiler will throw the following error&#8230;</p>
<h3>Compiler Error Message: CS1513: } expected</h3>
<p>Fortunately the solution is simple. Prepend the reserved class keyword with an @ character. This lets the compiler know that you want to use the word class as a property within it&#8217;s currently applied context.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #f7f4ef;">&lt;%=</span> Html.<span style="color: #87bcd2;">ActionLink</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;My Link&quot;</span>, <span style="color: #acacac;">&quot;MyLink&quot;</span>, <span style="color: #87BCD2;">null</span>, <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> @<span style="color: #D6618C;">class</span> <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;test&quot;</span> <span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">%&gt;</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/BW_3-fWsScI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/css-class-property-asp-net-mvc-htmlattributes/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/css-class-property-asp-net-mvc-htmlattributes/</feedburner:origLink></item>
		<item>
		<title>Creating a simple RSS feed using ASP.NET MVC</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/il2_AMwmK5w/</link>
		<comments>http://www.wduffy.co.uk/blog/creating-a-simple-rss-feed-using-aspnet-mvc/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:46:45 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[MVC]]></category>

		<category><![CDATA[RSS]]></category>

		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=458</guid>
		<description><![CDATA[Looking to serve an rss document via your ASP.NET MVC controller? Here is a simple, quick means to do so. If you are looking for a much neater, smarter, reusable method of serving rss from your controllers check out my ASP.NET MVC RssResult post (coming soon).
For the demonstrations below I will be using a generic [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">L</span>ooking to serve an rss document via your ASP.NET MVC controller? Here is a simple, quick means to do so. If you are looking for a much neater, smarter, reusable method of serving rss from your controllers check out my <a href="/blog/rssresult-aspnet-mvc-rss-actionresult/">ASP.NET MVC RssResult</a> post (coming soon).</p>
<p>For the demonstrations below I will be using a generic List&lt;News&gt;. News is a simple class with common news properties, we will be using Title, Description and Url. Although you can change the view to handle your own model easily enough.</p>
<p>Create a view named Feed, as you would normally for an HTML view. However, in this case change the ContentType property to &#8220;text/xml&#8221;. Sample code is listed below. Notice the &lt;?xml tag is butted up against the page declaration. This is necessary because an xml document&#8217;s first character must be the beginning of the doctype declaration.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&nbsp;
&lt;%@ Page Language=&quot;C#&quot; =&quot;text/xml&quot; Inherits=&quot;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;Domain.Model.News&gt;&gt;&quot; %&gt;&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;rss version=&quot;2.0&quot;&gt;
  &lt;channel&gt;
    &lt;title&gt;William Duffy's News&lt;/title&gt;
    &lt;link&gt;http://www.wduffy.co.uk&lt;/link&gt;
    &lt;description&gt;The latest news and stuff from the exciting life of William Duffy&lt;/description&gt;
    &lt;% foreach (var news in Model) { %&gt;
    &lt;item&gt;
      &lt;title&gt;&lt;%= news.Title %&gt;&lt;/title&gt;
      &lt;link&gt;http://www.wduffy.co.uk/News/Details/&lt;%= news.Url %&gt;&lt;/link&gt;
      &lt;description&gt;&lt;%= news.Description %&gt;&lt;/description&gt;
    &lt;/item&gt;
    &lt;% } %&gt;
  &lt;/channel&gt;
&lt;/rss&gt;</pre></td></tr></table></div>

<p>In your controller, pass the IEnumerable collection of items that are to be represented in the rss document to the view.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #87BCD2;">public</span> ActionResult Feed<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>
<span style="color: #f7f4ef;">&#123;</span>
    NewsCollection news <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> NewsService<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">GetByLatest</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
    <span style="color: #87BCD2;">return</span> View<span style="color: #f7f4ef;">&#40;</span>news<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<p>And that&#8217;s it. You can now request your rss document at http://www.yourdomain.com/Controller/Feed.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/il2_AMwmK5w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/creating-a-simple-rss-feed-using-aspnet-mvc/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/creating-a-simple-rss-feed-using-aspnet-mvc/</feedburner:origLink></item>
		<item>
		<title>ASP.NET MVC root url’s with generic routing</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/xcyHCvptGb8/</link>
		<comments>http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 11:27:51 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[MVC]]></category>

		<category><![CDATA[IRouteConstraint]]></category>

		<category><![CDATA[Reflection]]></category>

		<category><![CDATA[Routing]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=423</guid>
		<description><![CDATA[Normally, the url to handle a contact view would look similar to /Home/Contact. I&#8217;m really not keen on that and wanted my top level view&#8217;s url to look like /Contact. This in itself is easy enough, you just create a route {action} and set that routes default controller value to { controller = &#8220;Root&#8221; }. [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">N</span>ormally, the url to handle a contact view would look similar to <strong>/Home/Contact</strong>. I&#8217;m really not keen on that and wanted my top level view&#8217;s url to look like <strong>/Contact</strong>. This in itself is easy enough, you just create a route {action} and set that routes default controller value to { controller = &#8220;Root&#8221; }. </p>
<p>However I wanted to keep the default route mapping &#8220;{controller}/{action}/{id}&#8221; to handle generic formats along with this top level action route. The problem with allowing both these situations is the routes to handle both will always match each other. Whichever comes first will try to handle the routing for either.</p>
<p>For example a request for url <strong>/Admin</strong> would be happily matched by the route {action} or {controller}/{action}. In this case, if we wanted the <strong>/Admin</strong> url to be handled by the AdminController, the {action} route would match the request and try to route it to RootController&#8217;s Admin action; which of course does not exists. If we swapped the order of these routes in the global.asax file then <strong>/Admin</strong> would be matched properly and routed to AdminController&#8217;s Index action. However, now a requested for url <strong>/Contact</strong> would result in the generic route assuming <strong>/Contact</strong> is a controller and routing the request to ContactController&#8217;s Index method. Which does not exist because Contact is in-fact an action in RootController.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">routes.<span style="color: #87bcd2;">MapRoute</span><span style="color: #f7f4ef;">&#40;</span>
    <span style="color: #acacac;">&quot;Root&quot;</span>,
    <span style="color: #acacac;">&quot;{action}&quot;</span>,
    <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> controller <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Root&quot;</span>, action <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Index&quot;</span> <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
routes.<span style="color: #87bcd2;">MapRoute</span><span style="color: #f7f4ef;">&#40;</span>
    <span style="color: #acacac;">&quot;Generic&quot;</span>,
    <span style="color: #acacac;">&quot;{controller}/{action}/{id}&quot;</span>,
    <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> controller <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Generic&quot;</span>, action <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Index&quot;</span>, id <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;&quot;</span> <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>The solution was to create a custom constraint on the root route. This constraint would check to see if a controller exists that matches the {action} parameter&#8217;s value. If it does then we know that a controller has been requested and that the root route should not handle it. This will result in the next route in the table <em>(the generic route)</em> being assessed to handle the request. </p>
<p>In order to implement this you simply create a new custom contraint object and inherit IRouteContraint. In the constructor of the custom contraint assess the assembly for all types that inherit from Controller and create a dictionary object to keep hold of them. Then, every time the route engine assesses the request by calling Match check the action that has been requested and see if there is a specific controller that has the same name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Web.Routing</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #f7f4ef;">;</span>
&nbsp;
<span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System.<span style="color: #87bcd2;">Web</span></span>.<span style="color: #87bcd2;">Mvc</span>
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> IsRootActionConstraint <span style="color: #f7f4ef;">:</span> IRouteConstraint
    <span style="color: #f7f4ef;">&#123;</span>
        <span style="color: #87BCD2;">private</span> Dictionary<span style="color: #f7f4ef;">&lt;</span><span style="color: #D6618C;">string</span>, Type<span style="color: #f7f4ef;">&gt;</span> _controllers<span style="color: #f7f4ef;">;</span>
&nbsp;
        <span style="color: #87BCD2;">public</span> IsRootActionConstraint<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            _controllers <span style="color: #f7f4ef;">=</span> Assembly
                                .<span style="color: #87bcd2;">GetCallingAssembly</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>
                                .<span style="color: #87bcd2;">GetTypes</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>
                                .<span style="color: #87bcd2;">Where</span><span style="color: #f7f4ef;">&#40;</span>type <span style="color: #f7f4ef;">=&gt;</span> type.<span style="color: #87bcd2;">IsSubclassOf</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">typeof</span><span style="color: #f7f4ef;">&#40;</span>Controller<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span>
                                .<span style="color: #87bcd2;">ToDictionary</span><span style="color: #f7f4ef;">&#40;</span>key <span style="color: #f7f4ef;">=&gt;</span> key.<span style="color: #87bcd2;">Name</span>.<span style="color: #87bcd2;">Replace</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;Controller&quot;</span>, <span style="color: #acacac;">&quot;&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region IRouteConstraint Members</span>
&nbsp;
        <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">bool</span> Match<span style="color: #f7f4ef;">&#40;</span>HttpContextBase httpContext, Route route, <span style="color: #D6618C;">string</span> parameterName, RouteValueDictionary values, RouteDirection routeDirection<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            <span style="color: #87BCD2;">return</span> <span style="color: #f7f4ef;">!</span>_controllers.<span style="color: #87bcd2;">Keys</span>.<span style="color: #87bcd2;">Contains</span><span style="color: #f7f4ef;">&#40;</span>values<span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">&quot;action&quot;</span><span style="color: #f7f4ef;">&#93;</span> <span style="color: #87BCD2;">as</span> <span style="color: #D6618C;">string</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>Now all that is left to do is pass a new instance of the custom constraint in the routing table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">routes.<span style="color: #87bcd2;">MapRoute</span><span style="color: #f7f4ef;">&#40;</span>
    <span style="color: #acacac;">&quot;Root&quot;</span>,                                                 
    <span style="color: #acacac;">&quot;{action}&quot;</span>,
    <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> controller <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Root&quot;</span>, action <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Index&quot;</span> <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> IsRootAction <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> IsRootActionConstraint<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">&#125;</span>  <span style="color: #008080; font-style: italic;">// Route Constraint</span>
<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
routes.<span style="color: #87bcd2;">MapRoute</span><span style="color: #f7f4ef;">&#40;</span>
    <span style="color: #acacac;">&quot;Generic&quot;</span>,
    <span style="color: #acacac;">&quot;{controller}/{action}/{id}&quot;</span>,
    <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> controller <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Generic&quot;</span>, action <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;Index&quot;</span>, id <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;&quot;</span> <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></td></tr></table></div>

<p>You will now be able to use much neater root url&#8217;s and still have the advantage of a generic route in your routing table <img src='http://www.wduffy.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/xcyHCvptGb8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/</feedburner:origLink></item>
		<item>
		<title>JSON serialization in ASP.NET</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/Sgk79V9Ydvg/</link>
		<comments>http://www.wduffy.co.uk/blog/json-serialization-in-aspnet/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 15:28:38 +0000</pubDate>
		<dc:creator>William</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[JSON]]></category>

		<category><![CDATA[Latitude]]></category>

		<category><![CDATA[Longitude]]></category>

		<category><![CDATA[Postcode]]></category>

		<category><![CDATA[Serialization]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=394</guid>
		<description><![CDATA[When working with JavaScript and ASP.NET together you will no doubt find yourself having to return objects from the server side domain to the client side domain. 
One common method of doing this is to return XML which represents the object you wish to return. You then map the values in the XML document to [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">W</span>hen working with JavaScript and ASP.NET together you will no doubt find yourself having to return objects from the server side domain to the client side domain. </p>
<p>One common method of doing this is to return XML which represents the object you wish to return. You then map the values in the XML document to your JavaScript object <em>(or parse the XML document manually)</em> at which point you can start working with it as normal. </p>
<p>However, a much easier option is to return JSON (JavaScript Object Notation). Using a framework such as jQuery you can simply retrieve the JSON and have concrete JavaScript object ready for manipulation.</p>
<p>This is highly advantageous when you are performing AJAX operations. For example, imagine you have a server-side postcode query system that returns latitude/longitude values for a submitted postcode. After the postcode is queried on the server a Location object is created which holds the latitude/longitude values to return to the client. This object would look like the following&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> Location
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">public</span> Location<span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">string</span> postcode, <span style="color: #D6618C;">double</span> latitude, <span style="color: #D6618C;">double</span> longitude<span style="color: #f7f4ef;">&#41;</span>
    <span style="color: #f7f4ef;">&#123;</span>
        Postcode <span style="color: #f7f4ef;">=</span> postcode<span style="color: #f7f4ef;">;</span>
        Latitude <span style="color: #f7f4ef;">=</span> latitude<span style="color: #f7f4ef;">;</span>
        Longitude <span style="color: #f7f4ef;">=</span> longitude<span style="color: #f7f4ef;">;</span>
    <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">string</span> Postcode <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> set<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">double</span> Latitude <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> set<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">double</span> Longitude <span style="color: #f7f4ef;">&#123;</span> get<span style="color: #f7f4ef;">;</span> set<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>To allow the client to receive the Location object we create an HttpHandler. In a production environment this HttpHandler would query your database for the submitted postcode. However in the example below we will use a mock object to demonstrate JSON serialization in action. <em>(Serialization is simply a means of representing an object in state. In this case the state will be text for transmission over the http protocol.)</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">using</span> <span style="color: #008080;">System</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Web.Script.Serialization</span><span style="color: #f7f4ef;">;</span>
&nbsp;
<span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> LocationHandler <span style="color: #f7f4ef;">:</span> IHttpHandler <span style="color: #f7f4ef;">&#123;</span>
&nbsp;
    <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">void</span> ProcessRequest <span style="color: #f7f4ef;">&#40;</span>HttpContext context<span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">&#123;</span>
&nbsp;
        <span style="color: #D6618C;">string</span> postcode <span style="color: #f7f4ef;">=</span> context.<span style="color: #87bcd2;">Request</span>.<span style="color: #87bcd2;">QueryString</span><span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">&quot;postcode&quot;</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// This is where you would query your database</span>
        <span style="color: #008080; font-style: italic;">// i.e. Assess postcode for valid data</span>
        <span style="color: #008080; font-style: italic;">// i.e  Create postcode database parameter @postcode</span>
        <span style="color: #008080; font-style: italic;">// i.e. SELECT latitude, longitude FROM tblGeocodes WHERE postcode = @postcode;&quot;</span>
        <span style="color: #008080; font-style: italic;">// However for this demo we will create a mock object</span>
&nbsp;
        <span style="color: #D6618C;">double</span> rnd <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> Random<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">NextDouble</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        Location loc <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> Location<span style="color: #f7f4ef;">&#40;</span>postcode, <span style="color: #D6618C;">55.95254</span> <span style="color: #f7f4ef;">*</span> rnd, <span style="color: #f7f4ef;">-</span><span style="color: #D6618C;">4.767455</span> <span style="color: #f7f4ef;">*</span> rnd<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
        JavaScriptSerializer serializer <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> JavaScriptSerializer<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        context.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">Write</span><span style="color: #f7f4ef;">&#40;</span>serializer.<span style="color: #87bcd2;">Serialize</span><span style="color: #f7f4ef;">&#40;</span>loc<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        context.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">ContentType</span> <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;application/json&quot;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
    <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">bool</span> IsReusable <span style="color: #f7f4ef;">&#123;</span>
        get <span style="color: #f7f4ef;">&#123;</span>
            <span style="color: #87BCD2;">return</span> false<span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
<span style="color: #f7f4ef;">&#125;</span></pre></td></tr></table></div>

<p>The code above creates the Location object. It then creates a JavaScriptSerializer object which it uses to serialize the Location object to the response stream. The result is a JSON representation of the Location object that can be sent to the client where JavaScript can recreate it. The JSON looks as follows</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{&quot;Postcode&quot;:&quot;pa16 8ng&quot;,&quot;Latitude&quot;:14.566054759251575,&quot;Longitude&quot;:-1.24110559757015}</pre></div></div>

<p>Note: You will need to ensure that you have a reference to the System.Web.Extensions dll. If you are using ASP.NET 3.5 this is already installed and available. If you are using ASP.NET 2.0 you will need to download <a href="http://www.asp.net/ajax/" target=-"_blank">ASP.NET AJAX</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&nbsp;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;JSON Location&lt;/title&gt;
    &lt;script src=&quot;jquery-1.3.2.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        $().ready(function() {
            $('#&lt;%=txtPostcode.ClientID%&gt;').keyup(function() {
                $.getJSON(&quot;LocationHandler.ashx&quot;, { postcode: this.value }, function(location) {                   
                    $(&quot;.postcode&quot;).text(location.Postcode);
                    $(&quot;.latitude&quot;).text(location.Latitude);
                    $(&quot;.longitude&quot;).text(location.Longitude);
                });
            });
        });
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
&nbsp;
    Get location for postcode: 
    &lt;asp:TextBox ID=&quot;txtPostcode&quot; MaxLength=&quot;8&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt;&lt;br /&gt;
&nbsp;
    Postcode: &lt;span class=&quot;postcode&quot;&gt;&lt;/span&gt;&lt;br /&gt;
    Latitude: &lt;span class=&quot;latitude&quot;&gt;&lt;/span&gt;&lt;br /&gt;
    Longitude: &lt;span class=&quot;longitude&quot;&gt;&lt;/span&gt;
&nbsp;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>I am using jQuery to perform the AJAX request, but you can use whatever javascript method/framework you prefer.</p>
<h3>IMPORTANT</h3>
<p>Retreiveing and working with JSON is perfectly safe to do when you know and trust the source. However, if you do not know the source that is serving the JSON this can be a potentially dangerous call. So in cases where you are not in full control or fully trust the JSON provider you should seek a different solution.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/Sgk79V9Ydvg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/json-serialization-in-aspnet/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/json-serialization-in-aspnet/</feedburner:origLink></item>
	</channel>
</rss>
