<?xml version="1.0" encoding="UTF-8"?>
<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>Tech News, Tech Resources, Technology Articles, Gadget News, Computer News &#187; Web Programming</title>
	<atom:link href="http://www.comteken.com/category/web-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.comteken.com</link>
	<description>IT news, software technology, IT resources, computer software, laptops, desktops,  information systems,  hardware technology, multimedia, Windows OS, linux clients, network solution, easy internet, essential guides, domains, webhosting, web program, database programming</description>
	<lastBuildDate>Fri, 27 Jan 2012 11:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Logging with ASP.NET MVC Action Filters</title>
		<link>http://www.comteken.com/web-programming/logging-with-asp-net-mvc-action-filters/</link>
		<comments>http://www.comteken.com/web-programming/logging-with-asp-net-mvc-action-filters/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:12:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=57</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/logging-with-asp-net-mvc-action-filters/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>Logging is a common Cross-Cutting Concern that many ASP.NET developers solve in the Global.asax file. Because MVC is built on top of ASP.NET you *could* tap into the same solution, but there is a better way. This article will show how easy it is to add logging to your web app using ASP.NET MVC Action [...]]]></description>
			<content:encoded><![CDATA[<p>Logging is a common <a rel="nofollow" href="http://en.wikipedia.org/wiki/Cross-cutting_concern">Cross-Cutting Concern</a> that many ASP.NET developers solve in the Global.asax file. Because MVC is built on top of ASP.NET you *could* tap into the same solution, but there is a better way. This article will show how easy it is to add logging to your web app using ASP.NET MVC Action Filters.</p>
<p>Action Filters give you the ability to run custom code before or after an action (or page) is hit. Applying action filters to your MVC app is simple because they are implemented as attributes that can be placed on a method (an individual Action), or a class (the entire Controller).</p>
<p>To show how easy this is, we&#8217;re going to take the out-of-the-box ASP.NET MVC template, and very slightly tweak it to begin logging. We&#8217;ll add one class (our custom Action Filter), and salt the existing pages with our new &#8220;LogRequest&#8221; attribute.</p>
<h3>Creating a Custom Action Filter</h3>
<p>To create your own action filter, you simply have to inherit from the base &#8220;ActionFilterAttribute&#8221; class that&#8217;s already a part of the MVC framework. To make this easier on myself, I&#8217;ve also implemented the IActionFilter interface so that Visual Studio can auto-generate my two methods for me.</p>
<p>At this point, my custom action filter looks like this:</p>
<div><span>public</span> <span>class</span> LogsRequestsAttribute : ActionFilterAttribute, IActionFilter<br />
{<br />
   <span>void</span> IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)<br />
   {<span><br />
       // I need to <span>do</span> something here&#8230;</span><br />
   }</p>
<p>   <span>void</span> IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)<br />
   {<span><br />
       // I need to <span>do</span> something here&#8230;</span><br />
   }<br />
}</div>
<p>The ActionFilterAttribute and IActionFilter interface comes from the System.Web.Mvc namespace.</p>
<div>
<p>It&#8217;s important to remember that this article was written (and the sample app was compiled) for ASP.NET MVC Preview 4. When the beta and release is eventually out, specifics of the article may not be 100% relevant. However, this capability should remain the same.</p></div>
<h3>Applying Our Custom Action Filter</h3>
<p>Now that we have created our action filter, we need to apply it to our Controllers or optionally, to our Actions. Because logging is something that you would likely want on all of your &#8220;pages&#8221;, we&#8217;ll simply add it at the controller level. Here is what we&#8217;ve added to the two existing controllers that were supplied in the ASP.NET MVC template.</p>
<div><span>// HandleError was already there&#8230;</span><br />
[HandleError]<br />
[LogRequest]<br />
<span>public</span> <span>class</span> HomeController : Controller<br />
{<br />
   &#8230;<br />
}<span></p>
<p>// HandleError was already there&#8230;</span><br />
[HandleError]<br />
[LogRequest]<br />
<span>public</span> <span>class</span> AccountController : Controller<br />
{<br />
   &#8230;<br />
}</div>
<p>That&#8217;s it! The ASP.NET MVC framework will automatically call our methods (OnActionExecuting and then OnActionExecuted) when a request comes in to any of those two controllers.</p>
<p>At this point, all we have to do is actually implement our logging code. Because I want to be able to easily report against my site&#8217;s activity, I&#8217;m going to log to a SQL database. Now, it&#8217;s important to note that action filters are executed synchronously (for obvious reasons), but I don&#8217;t want the user to have to wait for my logging to happen before he can enjoy my great site. So, I&#8217;m going to use the <a rel="nofollow" href="http://www.eggheadcafe.com/articles/20050818.asp">fire and forget</a> design pattern.</p>
<p>When we&#8217;re all done, this is what our logger will look like:</p>
<div><span>// By the way, I&#8217;m <span>using</span> the Entity Framework <span>for</span> fun.</span><br />
<span>public</span> <span>class</span> LogRequestAttribute : ActionFilterAttribute, IActionFilter<br />
{<br />
   <span>private</span> <span>static</span> LoggerDataStoreEntities DataStore = <span>new</span> LoggerDataStoreEntities();</p>
<p>   <span>void</span> IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)<br />
   {<br />
       ThreadPool.QueueUserWorkItem(<span>delegate</span><br />
       {<br />
           DataStore.AddToSiteLog(<span>new</span> SiteLog<br />
           {<br />
               Action = filterContext.ActionMethod.Name,<br />
               Controller = filterContext.Controller.ToString(),<br />
               TimeStamp = filterContext.HttpContext.Timestamp,<br />
               IPAddress = filterContext.HttpContext.Request.UserHostAddress,<br />
           });</p>
<p>           DataStore.SaveChanges();<br />
       });<br />
   }<br />
}</p></div>
<div><span>Update: Sep 12 2008</span>As was mentioned in a comment below, to get the &#8220;ActionMethod&#8221; in Preview 5, you would use:</p>
<div>filterContext.RouteData.Values[<span>"action"</span>]</div>
</div>
<h3>Conclusion</h3>
<p>There are a lot of features in MVC that could each merrit their own articles, but Action Filters are definately one feature that shows off the advantages of the MVC design pattern. Action Filters make perfect sense for cross-cutting concerns like logging, but you can get creative with how and why you use them.</p>
<p>This article isn&#8217;t here to show you the best way to do logging, but rather how and why you would use ASP.NET MVC Action Filters. Here&#8217;s the source code, play around with it: <a href="http://www.comteken.com/wp-admin/Articles/UserFile.aspx?FileID=cca9988e-4c51-44d0-b457-6f60946b8095">MVC_CustomActionFilter_Logging.zip</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;bodytext=Logging%20is%20a%20common%20Cross-Cutting%20Concern%20that%20many%20ASP.NET%20developers%20solve%20in%20the%20Global.asax%20file.%20Because%20MVC%20is%20built%20on%20top%20of%20ASP.NET%20you%20%2Acould%2A%20tap%20into%20the%20same%20solution%2C%20but%20there%20is%20a%20better%20way.%20This%20article%20will%20show%20how%20easy%20it%20is%20to%20a" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;notes=Logging%20is%20a%20common%20Cross-Cutting%20Concern%20that%20many%20ASP.NET%20developers%20solve%20in%20the%20Global.asax%20file.%20Because%20MVC%20is%20built%20on%20top%20of%20ASP.NET%20you%20%2Acould%2A%20tap%20into%20the%20same%20solution%2C%20but%20there%20is%20a%20better%20way.%20This%20article%20will%20show%20how%20easy%20it%20is%20to%20a" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;t=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;annotation=Logging%20is%20a%20common%20Cross-Cutting%20Concern%20that%20many%20ASP.NET%20developers%20solve%20in%20the%20Global.asax%20file.%20Because%20MVC%20is%20built%20on%20top%20of%20ASP.NET%20you%20%2Acould%2A%20tap%20into%20the%20same%20solution%2C%20but%20there%20is%20a%20better%20way.%20This%20article%20will%20show%20how%20easy%20it%20is%20to%20a" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;Title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;headline=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=Logging%20is%20a%20common%20Cross-Cutting%20Concern%20that%20many%20ASP.NET%20developers%20solve%20in%20the%20Global.asax%20file.%20Because%20MVC%20is%20built%20on%20top%20of%20ASP.NET%20you%20%2Acould%2A%20tap%20into%20the%20same%20solution%2C%20but%20there%20is%20a%20better%20way.%20This%20article%20will%20show%20how%20easy%20it%20is%20to%20a" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;bm_description=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;t=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;h=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;story_title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;title=Logging%20with%20ASP.NET%20MVC%20Action%20Filters" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Logging%20with%20ASP.NET%20MVC%20Action%20Filters%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Flogging-with-asp-net-mvc-action-filters%2F&amp;submitHeadline=Logging%20with%20ASP.NET%20MVC%20Action%20Filters&amp;submitSummary=Logging%20is%20a%20common%20Cross-Cutting%20Concern%20that%20many%20ASP.NET%20developers%20solve%20in%20the%20Global.asax%20file.%20Because%20MVC%20is%20built%20on%20top%20of%20ASP.NET%20you%20%2Acould%2A%20tap%20into%20the%20same%20solution%2C%20but%20there%20is%20a%20better%20way.%20This%20article%20will%20show%20how%20easy%20it%20is%20to%20a&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/logging-with-asp-net-mvc-action-filters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Model Binders in ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-2/</link>
		<comments>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:11:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=55</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-2/"><img align="left" hspace="5" width="100" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=ccf389f4-da2a-4f13-9870-0cef371ec5aa" class="alignleft wp-post-image tfe" alt="MVC Model Binders Demo Screen Shot" title="MVC Model Binders Demo Screen Shot" /></a>Hot off the presses, and new to ASP.NET MVC (Preview 5) is an awesome capability that (in my opinion) revolutionizes the way we design web applications. This feature is being touted (by me) as &#8220;the ViewState for MVC&#8221;. First, the Challenge A common challenge of web applications is passing complex types and (more difficultly) stateful [...]]]></description>
			<content:encoded><![CDATA[<p>Hot off the presses, and new to ASP.NET MVC (Preview 5) is an awesome capability that (in my opinion) revolutionizes the way we design web applications. This feature is being touted (by me) as &#8220;the ViewState for MVC&#8221;.</p>
<h3>First, the Challenge</h3>
<p>A common challenge of web applications is passing complex types and (more difficultly) stateful objects from one page to another. This difficulty is due to the disconnected nature of the web. There have been many attempts to solve this problem (sessions, ViewState, etc), but ASP.NET MVC&#8217;s new &#8220;Model Binders&#8221; functionality definitely takes the gold medal for being the cleanest, simplest and most powerful.</p>
<p>We&#8217;re going to build a couple of simple MVC pages, one that display a list of customers and the other displays their address details. You can download the solution at the end of the article (compiled for Visual Studio 2008 SP1 / ASP.NET MVC Preview 5).</p>
<h3>What Are the Benefits?</h3>
<p>ASP.NET MVC Model Binders give you the following benefits:</p>
<ul>
<li>Complete control over the deserialization of complex types passed into your Action methods. This in itself has many sub-benefits: less bloat in your HTML and in passing objects back to the server, cleaner URL&#8217;s, etc.</li>
<li>Unified and testable code. This feature really holds to the &#8220;Single Responsibility&#8221; (or separation of concerns) principle. This one place is where you will handle restoring your objects from the <a rel="nofollow" href="http://en.wikipedia.org/wiki/Memento_pattern">memento</a> you come up with.</li>
</ul>
<div>
<p>The idea of using a ModelBinder to pass tokenized objects that live in your data model is not the typical usage. You could use them to define how other complex objects that aren&#8217;t serialized into the query string get passed into your methods, but purpose of this article is to show a more extreme example to demonstrate it&#8217;s power and capabilities.</p>
<p>A more common usage for ModelBinders would be to build your complex object from a form post. Example:</p>
<div><span>public</span> <span>override</span> <span>object</span> GetValue(ControllerContext ctx,<br />
   <span>string</span> modelName, Type modelType, ModelStateDictionary state)<br />
{<br />
   Customer customer = <span>new</span> Customer();</p>
<p>   customer.FirstName = ctx.HttpContext.Request[<span>"FirstName"</span>];</p>
<p>   customer.LastName = ctx.HttpContext.Request[<span>"LastName"</span>];<span></p>
<p>   // &#8230; other properties &#8230;</span></p>
<p>   <span>return</span> customer;<br />
}</div>
</div>
<p>By the end of this article, we&#8217;ll see how easy it is pass complex &#8220;Customer&#8221; objects around our application and we&#8217;ll talk about how to create unit tests to ensure the quality of our methods. Our customer information will be stored in a SQL database, and the customer class will be generated by the LINQ to SQL as a concrete representation of a record in our database.</p>
<p>Before we dive into our application, let&#8217;s take a look at a snippet of code and see how MVC model binders make a difference.</p>
<h3>MVC ModelBinder vs. Traditional ASP.NET</h3>
<p>To demonstrate this functionality simply, consider the following scenario. You&#8217;re looking at the &#8220;List&#8221; page for your users. The list of users is in an HTML &lt;table&gt;, and the first column is a link to that user&#8217;s &#8220;Edit&#8221; page.</p>
<p>Let&#8217;s assume our link will produce the following URL: &#8220;/Users/Edit.aspx?UserID=123&#8243;. In traditional ASP.NET, we would probably have the following code at the top of our page.</p>
<div><span>private</span> <span>void</span> Page_Load(<span>object</span> sender, EventArgs e)<br />
{<br />
   <span>int</span> userID = 0;</p>
<p>   <span>if</span> (<span>int</span>.TryParse(Request[<span>"UserID"</span>], <span>out</span> userID) == <span>false</span>)<br />
   {<span><br />
       // User ID wasn&#8217;t supplied, so redirect <span>out</span>.</span><br />
   }</p>
<p>   User currentUser = GetUserFromDatabase(userID);</p>
<p>   <span>if</span> (currentUser == <span>null</span>)<br />
   {<span><br />
       // Bad User ID, so redirect <span>out</span>.</span><br />
   }<span></p>
<p>   // &#8230;</span><br />
}</div>
<p>This may not seem that bad, but if you think about it, we&#8217;ll have to re-write this code in every page that we want to pass a user object to and for every parameter that we need to have passed to the page. This approach isn&#8217;t very object oriented, and you&#8217;re constantly reminded of the fact that you&#8217;re just grabbing data out of the query string.</p>
<p>Now, notice the difference between what you have to do in traditional ASP.NET, and ASP.NET MVC (thanks to Model Binders):</p>
<div><span>public</span> ActionResult Edit(User currentUser)<br />
{<br />
   <span>if</span> (currentUser == <span>null</span>)<br />
   {<span><br />
       // User not supplied, so redirect <span>out</span>.</span><br />
   }<span></p>
<p>   // &#8230;</span><br />
}</div>
<p>Notice how much simpler that was? OK, so you might be wondering &#8220;where is the code?&#8221;&#8230; &#8220;How is the &#8216;currentUser&#8217; parameter being passed in?&#8221; Now it&#8217;s time to see the magic.</p>
<h3>Creating and Registering Model Binders</h3>
<p>Because ASP.NET MVC is designed to make things simple and easy to use, creating and registering your own Model Binders is a quick task. There are really only two things you have to do to handle the &#8216;serialization / deserialization&#8217; part, and there is one simple line of code to register your custom model binder with the MVC framework. Let&#8217;s break it down here.</p>
<p>First of all, we need to decide how we want to &#8216;tokenize&#8217; our object (meaning, what string representation do we want to make so that we can get our object back later). You currently have three options here, but I would like to see a fourth one by the time MVC goes live (I&#8217;ll explain in a minute).</p>
<p>Essentially, the three choices you have all boil down to the same thing:</p>
<ul>
<li>Make your class implement the IConvertible interface, and provide your code for how it should &#8216;convert&#8217; to a string.</li>
<li>Make your class implement the IFormattable interface, and do the same as the above.</li>
<li>Or, simply override &#8220;ToString&#8221; and put your logic there.</li>
</ul>
<p>I&#8217;m going to use this third approach because it&#8217;s simple, but you certainly could go with one of the above. In some cases, it would be best to use the first choice (the IConvertible). You may want to choose this so that you can free up your &#8220;ToString&#8221; method to provide a user-friendly looking string representation of your object, and then use the ConvertTo method to provide your memento.</p>
<p>Example: &#8216;ToString()&#8217; might return &#8220;Khouri, Timothy&#8221;, and &#8216;ConvertTo()&#8217; might return &#8216;ID:1234&#8242;.</p>
<div>
<p>I mentioned that I would like to see a fourth option soon, and this one I think is the most powerful. As you will see below, ModelBinders give you the option to &#8220;restore&#8221; your object from the string token that is provided. I would like to also see the code that converts *to* that token in this same model binder.</p>
<p>There are many great reasons for this, but the most important I can think of is to give you (the developer) the ability to convert classes that you didn&#8217;t write into whatever format you want. i.e.: DateTime to serialize as &#8220;yyyyMMdd&#8221;.</p></div>
<p>Because my &#8220;Customer&#8221; class is being auto-generated by LINQ-to-SQL, I&#8217;m going to create a class file that extends it a little to provide my own &#8220;ToString&#8221; method. This is not an &#8220;extension method&#8221;, but rather just a partial class file. Here&#8217;s my code:</p>
<div><span>public</span> <span>partial</span> <span>class</span> Customer<br />
{<br />
   <span>public</span> <span>override</span> <span>string</span> ToString()<br />
   {<span><br />
       // I could just <span>return</span> <span>&#8220;ID.ToString()&#8221;</span>, but I</span><span><br />
       // want to be a little silly to prove that I have full</span><span><br />
       // control over how my memento <span>is</span> going to look <img src='http://www.comteken.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
<p>       <span>return</span> Convert.ToBase64String(<br />
               BitConverter.GetBytes(<span>this</span>.ID)<br />
           ).Replace(<span>&#8220;=&#8221;</span>, <span>&#8220;_&#8221;</span>);<br />
   }<br />
}</div>
<p>Basically, this will convert any instance of a Customer class to a string that looks something like &#8220;CwAAB0__&#8221;. Now, I need to make my ModelBinder that will take that string, convert it back to the ID of the Customer, and then re-create the Customer object (which in this case, will be a simple task for LINQ-to-SQL).</p>
<p>As was mentioned before, creating our own custom ModelBinder is very simple. We&#8217;re going to inherit from &#8220;DefaultModelBinder&#8221;, and then override the &#8220;ConvertType&#8221; method. The &#8216;value&#8217; object that gets passed in could be a string, or an array of strings, so I&#8217;ll have to check for that. Here&#8217;s the code:</p>
<div><span>public</span> <span>class</span> MyCustomerBinder : DefaultModelBinder<br />
{<br />
   <span>protected</span> <span>override</span> <span>object</span> ConvertType(CultureInfo culture, <span>object</span> value, Type destinationType)<br />
   {<span><br />
       // This <span>class</span> <span>is</span> only meant to convert Customer objects.</span><br />
       <span>if</span> (destinationType != <span>typeof</span>(Customer))<br />
       {<br />
           <span>return</span> <span>base</span>.ConvertType(culture, value, destinationType);<br />
       }<span></p>
<p>       // Get the ID that <span>is</span> being passed <span>in</span>.</span><br />
       <span>string</span> customerIDString = value <span>as</span> <span>string</span>;</p>
<p>       <span>if</span> (customerIDString == <span>null</span> &amp;&amp; value <span>is</span> <span>string</span>[])<br />
       {<br />
           customerIDString = ((<span>string</span>[])value)[0];<br />
       }</p>
<p>       customerIDString = customerIDString.Replace(<span>&#8220;_&#8221;</span>, <span>&#8220;=&#8221;</span>);</p>
<p>       <span>int</span> customerID = BitConverter.ToInt32(Convert.FromBase64String(customerIDString), 0);<span></p>
<p>       // Grab the customer <span>from</span> the database.</span><br />
       <span>return</span> MyDataAccessLayer.GetCustomerByID(customerID);<br />
   }<br />
}</div>
<p>That&#8217;s it! We&#8217;ve now created a ModelBinder that will tell MVC how to convert that token back into a real, tangible Customer object. Next we&#8217;ll see how to register and use our custom parameter converting model binder.</p>
<p>In our global application file (Global.asax), we are already registering our routing information for MVC (which tells the framework where to look for our Controllers and Actions). Now we&#8217;re going to add this simple line of code that tells the MVC framework to how to reconstitute our Customer class. Here&#8217;s the code:</p>
<div><span>// Register our ModelBinder.</span><br />
ModelBinders.Binders.Add(<span>typeof</span>(Customer),<br />
   <span>new</span> MyCustomerBinder());</div>
<h3>Seeing the Results</h3>
<p>There is no better way to fully appreciate what is happening here other than seeing the result for yourself. In my &#8216;list customers&#8217; page, I&#8217;m building the link to the details page in a very object-oriented way:</p>
<div><span>&lt;%</span><br />
   <span>foreach</span> (Customer customer <span>in</span> <span>this</span>.ViewData.Model)<br />
   {<br />
       <span>this</span>.Writer.Write(<span>&#8220;&lt;li&gt;&#8221;</span>);<span></p>
<p>       // Create a link that poitns to the <span>&#8220;Details&#8221;</span> method, passing</span><span><br />
       // <span>in</span> the *customer* <span>object</span>. The link should display the full</span><span><br />
       // name of the customer.</span><br />
       <span>this</span>.Writer.Write(<span>this</span>.Html.ActionLink&lt;HomeController&gt;(<br />
           c =&gt; c.Details(customer), customer.FullName));</p>
<p>       <span>this</span>.Writer.Write(<span>&#8220;&lt;/li&gt;&#8221;</span>);<br />
   }<br />
<span>%&gt;</span></div>
<p>Here&#8217;s is what our page will look like:</p>
<p><img title="MVC Model Binders Demo Screen Shot" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=ccf389f4-da2a-4f13-9870-0cef371ec5aa" alt="MVC Model Binders Demo Screen Shot" /></p>
<p>And here is the details page. Notice the URL. If you scroll up you&#8217;ll see that I never told MVC to call the parameter &#8220;targetCustomer&#8221;, but MVC automatically figured that out because that&#8217;s what the parameter is named in my &#8220;Details&#8221; method.</p>
<p><img title="MVC Model Binders Demo Screen Shot (Details Page)" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=c2cebfd3-b51d-4219-9c2f-34cf65960526" alt="MVC Model Binders Demo Screen Shot (Details Page)" /></p>
<h3>Testing Your Converter</h3>
<p>As I stated above, I wanted to mention why this approach is good in terms of testability. Basically, because this bit of functionality is so small and specific, it is fully testable. You can use your favorite &#8216;mock&#8217; framework to create a FakeHttp request with your token in the query string, and see if it correctly creates the Customer class.</p>
<p>As a side note: don&#8217;t forget that my model binder is hitting the data access layer, so if you are going to create unit tests you&#8217;ll want to also mock the DAL as well.</p>
<p>And now, here is the solution (compiled for Visual Studio 2008 SP1, ASP.NET MVC Preview 5): <a href="http://www.comteken.com/wp-admin/Articles/UserFile.aspx?FileID=9b82536f-717c-42cf-8397-b7fae5380985">SingingEels_MVC_ModelBinders.zip</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;bodytext=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;notes=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;annotation=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;Title=Model%20Binders%20in%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Model%20Binders%20in%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;headline=Model%20Binders%20in%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;bm_description=Model%20Binders%20in%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Model%20Binders%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;h=Model%20Binders%20in%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Model%20Binders%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;story_title=Model%20Binders%20in%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Model%20Binders%20in%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-2%2F&amp;submitHeadline=Model%20Binders%20in%20ASP.NET%20MVC&amp;submitSummary=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Driven Development with ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc-2/</link>
		<comments>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:10:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=53</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc-2/"><img align="left" hspace="5" width="100" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=b62d323a-435a-4d72-8757-cb1afeafefbc" class="alignleft wp-post-image tfe" alt="New ASP.NET MVC Preview 5 project in Visual Studio 2008" title="New ASP.NET MVC Preview 5 project in Visual Studio 2008" /></a>One of the biggest benefits of MVC is it&#8217;s direct link to Test Driven Development. Because of some of the new features of ASP.NET MVC Preview 5 (ModelBinders in particular), testing your Action methods is even easier. This article will demonstrate how easy it is to ensure the quality of your MVC app with unit [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest benefits of MVC is it&#8217;s direct link to Test Driven Development. Because of some of the new features of ASP.NET MVC Preview 5 (ModelBinders in particular), testing your Action methods is even easier. This article will demonstrate how easy it is to ensure the quality of your MVC app with unit tests.</p>
<p>For those of you who are not familiar with <a rel="nofollow" href="http://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development</a> (TDD) or it&#8217;s benefits, consider the following scenario. Let&#8217;s assume that you have been contracted to build a site that deals with customer management. One of the client&#8217;s business rules is that customers must have at least a first name, last name and a zip code. This is a fairly straight forward request that will help us easily get into TDD.</p>
<h3>Creating MVC Apps With Unit Tests</h3>
<p>To prove that MVC is meant for testing, the Visual Studio template for a new ASP.NET MVC (Preview 5) project will automatically ask if you want to include a Unit Test project as well.</p>
<p>The default Test Project that comes with the ASP.NET MVC Visual Studio template is pretty basic, and includes a test class for each of the default Controller&#8217;s that come with your MVC app.</p>
<p>So, the steps to start our project are very simple:</p>
<ol>
<li>File -&gt; New -&gt; Project</li>
<li>ASP.NET MVC Web Application</li>
<li>Yes, create a unit test project.</li>
</ol>
<p><img title="New ASP.NET MVC Preview 5 project in Visual Studio 2008" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=b62d323a-435a-4d72-8757-cb1afeafefbc" alt="New ASP.NET MVC Preview 5 project in Visual Studio 2008" /> <img title="Create Unit Test Project in Visual Studio 2008" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=f9fb574e-e466-4c27-8b3d-76f914db87d0" alt="Create Unit Test Project in Visual Studio 2008" /></p>
<h3>Business Rules as Tests</h3>
<p>So, after learning what our business rules are, our next step in TDD is to express those business rules in the form of unit tests. To do this, we&#8217;ll add a class to our Test Project that will handle testing all things &#8220;Customers&#8221; related. We&#8217;ll need to decorate the class with the &#8220;TestClass&#8221; attribute, and each method with the &#8220;TestMethod&#8221; attribute. Our &#8220;customers test class&#8221; should look like this:</p>
<div><span>// The name of the <span>class</span> doesn&#8217;t matter, but <span>this</span> name</span><span><br />
// makes sense to me.</span><br />
[TestClass]<br />
<span>public</span> <span>class</span> TestCustomersController<br />
{<span><br />
   // Also, the name of the method doesn&#8217;t matter,</span><span><br />
   // but again, <span>this</span> name makes sense <span>for</span> our goal.</span><br />
   [TestMethod]<br />
   <span>public</span> <span>void</span> EnsureRequiredFields()<br />
   {<span><br />
       // Our test code will go here.</span><br />
   }<br />
}</div>
<p>Now that we have a stub for our tests, we need to do a *little* bit of implementing our code in our CustomersController class in our ASP.NET MVC app. All we are going to do is create the method stub that is responsible for creating new customers. Once we do that, we can come back and finish our test method.</p>
<p>I mentioned earlier in the article that using a <a rel="nofollow" href="http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx">ModelBinder</a> will make writing tests easier, and that may seem confusing at first. ModelBinders are run by the ASP.NET MVC framework, and will not be used by our Test Project, so how can they help in writing unit tests?</p>
<p>The answer is simple: Object Oriented Programming. Basically, because ModelBinders allow me to write my ASP.NET MVC Action Methods with complex objects as parameters, I can test against them in a much more OO fashion. Example:</p>
<div><span>// Not a very OO way of doing things, and also not</span><span><br />
// fun to test.</span><br />
<span>public</span> ActionResult Create()<br />
{<span><br />
   // Pull values <span>from</span> HttpRequest?</span><br />
   <span>if</span> (<span>string</span>.IsNullOrEmpty(Request[<span>"firstName"</span>]))<br />
   {<span><br />
       // <span>return</span> some sort of validation error.</span><br />
   }<br />
}<span></p>
<p>// Whereas <span>this</span> <span>is</span> a very OO way of doing things, and</span><span><br />
// <span>is</span> also very easy to write tests against!</span><br />
<span>public</span> ActionResult Create(Customer newCustomer)<br />
{<br />
   <span>if</span> (<span>string</span>.IsNullOrEmpty(newCustomer.FirstName))<br />
   {<span><br />
       // <span>return</span> some sort of validation error.</span><br />
   }<br />
}</div>
<p>So now we are ready to write our tests, but first we should mention a little &#8220;gotcha&#8221; that needs to be addressed. Because I only want to test my business rules, and not the code that ultimately writes the customer record to the database, I need to prevent my code from actually running the &#8220;store in database&#8221; part of my code. This is known as <a rel="nofollow" href="http://en.wikipedia.org/wiki/Virtual_mock">&#8220;mocking&#8221;</a>.</p>
<div>
<p>While there are many great mock frameworks out there (<a rel="nofollow" href="http://code.google.com/p/moq/">Moq</a>, <a rel="nofollow" href="http://www.nmock.org/">NMock</a>, <a rel="nofollow" href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a>, and <a rel="nofollow" href="http://www.google.com/search?hl=en&amp;q=.net+mock+frameworks">many more</a>), I&#8217;m using <a rel="nofollow" href="http://www.typemock.com/">Typemock Isolator</a> for this article. Why? &#8211; Because.</div>
<h3>Testing One Step at a Time</h3>
<p>There are three pieces to our business rule that we are going to test individually: Required first name, last name and zip. To do this, we&#8217;re going to create an instance of our CustomersController, and call Create method. We&#8217;re going to pass in a customer object that *should fail validation*, and then we&#8217;ll check to make sure that, in fact, the Create method did fail.</p>
<p>This may seem odd (to hope for failure), but again remember that we want to test to make sure that validation is working, so we have to pass in bad data and look for the validation to come back.</p>
<p>Here&#8217;s our code that we&#8217;ll put inside of our &#8220;EnsureRequiredFields&#8221; method mentioned earlier in the article:</p>
<div>Customer newCustomer;</p>
<p>CustomersController controller = <span>new</span> CustomersController();<span></p>
<p>// We don&#8217;t want to actually hit the database, so we&#8217;ll just tell the test framework to ignore <span>this</span> method.</span><br />
MockManager.Mock(<span>typeof</span>(DataStore)).ExpectAlways(<span>&#8220;InsertCustomer&#8221;</span>);</p>
<p>{ // Test <span>for</span> required <span>&#8220;FirstName&#8221;</span>.<br />
   controller.ViewData.ModelState.Clear();</p>
<p>   newCustomer = <span>new</span> Customer<br />
   {<br />
       FirstName = <span>&#8220;&#8221;</span>,<br />
       LastName = <span>&#8220;Smith&#8221;</span>,<br />
       Zip = <span>&#8220;34275&#8243;</span>,    <br />
   };</p>
<p>   controller.Create(newCustomer);<span></p>
<p>   // Make sure that our validation found the error!</span><br />
   Assert.IsTrue(controller.ViewData.ModelState.Count == 1, <span>&#8220;FirstName must be required.&#8221;</span>);<br />
}<span></p>
<p>// then we&#8217;ll test <span>for</span> LastName, and then Zip <span>in</span> the same way.</span></div>
<p>Now, when we run our tests in Visual Studio we will fail until we code the appropriate validation in our Create method. Here&#8217;s what that will look like:</p>
<p><img title="Failed MVC unit test requiring FirstName" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=6c169f9b-d180-4cd8-a018-7ce3697aeba9" alt="Failed MVC unit test requiring FirstName" /></p>
<p>Now you keep coding your app until you pass that test (which in this case should be pretty easy). So, once we add this code to our Create method, we&#8217;ll be good to go:</p>
<div><span>if</span> (<span>string</span>.IsNullOrEmpty(newCustomer.FirstName))<br />
{<br />
   <span>this</span>.ViewData.ModelState.AddModelError(<span>&#8220;FirstName&#8221;</span>, <span>&#8220;&#8221;</span>, <span>&#8220;&#8216;FirstName&#8217; <span>is</span> a required field.&#8221;</span>);</p>
<p>   isValid = <span>false</span>;<br />
}<span></p>
<p>// also test <span>for</span> LastName and Zip&#8230;</span></p>
<p><span>if</span> (isValid == <span>false</span>)<br />
{<br />
   <span>return</span> View();<br />
}</div>
<h3>Conclusion</h3>
<p>Hopefully this article has been able to demonstrate how easy it is to start down the path of TDD with the ASP.NET MVC framework. The demo project for this article can be downloaded here: <a href="http://www.comteken.com/wp-admin/Articles/UserFile.aspx?FileID=61aa06f8-fc3a-4009-ab1c-a68a27a4e2fe">MvcDemo_Testing_CreateCustomer.zip</a>.</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;bodytext=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;notes=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;t=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;annotation=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;Title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;headline=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;bm_description=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;t=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;h=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;story_title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Test%20Driven%20Development%20with%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc-2%2F&amp;submitHeadline=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;submitSummary=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC AJAX Sites That Gracefully Degrade</title>
		<link>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade-2/</link>
		<comments>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade-2/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:09:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=51</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade-2/"><img align="left" hspace="5" width="100" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=23d3731b-07bd-46d6-b74e-c215b65f696c" class="alignleft wp-post-image tfe" alt="ASP.NET MVC - AJAX application that gracefully degrades part 1" title="ASP.NET MVC - AJAX application that gracefully degrades part 1" /></a>As AJAX-enabled web sites continue to grow in popularity, the development community continues to try to solve the common problems of using AJAX. One big concern that used to require a lot of work is that of gracefully degrading your AJAX site for non-capable browsers. Thankfully, the ASP.NET MVC framework makes this an easy task. [...]]]></description>
			<content:encoded><![CDATA[<p>As AJAX-enabled web sites continue to grow in popularity, the development community continues to try to solve the common problems of using AJAX. One big concern that used to require a lot of work is that of gracefully degrading your AJAX site for non-capable browsers. Thankfully, the ASP.NET MVC framework makes this an easy task.</p>
<p>This article will demonstrate some basic AJAX functionality that comes with the ASP.NET MVC framework. We&#8217;ll also see how simple it is to get the same usefulness of our web application even if we disable JavaScript capabilities in our browser.</p>
<p>Our demo site will have a &#8220;news&#8221; section where users can view the latest news, and subscribe to our newsletter. This functionality will be provided through AJAX calls. Our goal is to achieve this same functionality for non-ajax-enabled browsers without duplicating a lot of work.</p>
<h3>AJAX Extensions in ASP.NET MVC</h3>
<p>If you&#8217;ve been playing around in ASP.NET MVC even a little, you likely have noticed how simple it is to use AJAX in your web app. Take for example creating a link to our &#8220;News List&#8221; page (which is actually in our demo app). To make a standard link that goes to the &#8220;News&#8221; controller and executes the &#8220;List&#8221; action would look something like this:</p>
<div><span>&lt;%</span><span>=</span> Html.ActionLink(<span>&#8220;View News&#8221;</span>, <span>&#8220;List&#8221;</span>, <span>&#8220;News&#8221;</span>) <span>%&gt;</span></div>
<p>But what would we do if we wanted that link to use AJAX to pull the latest news and update a simple &lt;div&gt; on our page? All we would have to do is use the &#8220;Ajax.ActionLink&#8221; method, and supply an extra parameter that has the &#8216;AJAX options&#8217; in there. Here&#8217;s what that would look like:</p>
<div><span>&lt;%</span><span>=</span><br />
   Ajax.ActionLink(<span>&#8220;View News&#8221;</span>, <span>&#8220;List&#8221;</span>, <span>&#8220;News&#8221;</span>,<br />
       <span>new</span> AjaxOptions<br />
       {<br />
           HttpMethod = <span>&#8220;POST&#8221;</span>,<br />
           UpdateTargetId = <span>&#8220;news-list&#8221;</span>,<br />
           InsertionMode = InsertionMode.Replace,<br />
       })<br />
<span>%&gt;</span></div>
<p>That code will build an HTML link that also has some JavaScript in the onclick event for handling the AJAX stuff. It&#8217;s important to know that since this is a regular old HTML link, if the JavaScript part fails (either due to a bug, or if scripts are disabled), then the browser will navigate to the URL as if there was no JavaScript at all. And that brings us to the next point!</p>
<h3>Coding Your ActionMethod to Detect AJAX Requests</h3>
<p>Because one of our main goals is not to have to duplicate a lot of work, we want the same code to run for the user if he&#8217;s using AJAX or not. The only part that has to change really is the &#8220;View&#8221; that gets sent back to the client. In order to accomplish this, we&#8217;re going to use the &#8220;IsMvcAjaxRequest&#8221; method that comes in the System.Web.Mvc.Ajax namespace (in the AjaxExtensions class).</p>
<div><span>Update: Oct 22 2008</span>Originally, I didn&#8217;t know about the &#8220;IsMvcAjaxRequest&#8221; method, so I had a code snippet here that showed how to make our own method by looking for the &#8220;__MVCASYNCPOST&#8221; form field. Due to a comment below, I&#8217;ve removed the code snippet that had the extension method here.</p>
<p>As was mentioned by <a rel="nofollow" href="http://www.lostintangent.com/">Jonathan Carter</a> (who&#8217;s a Technical Evangelist for Microsoft), the MVC Beta already includes the extension method above. Now all we have to do is call the &#8220;IsMvcAjaxRequest&#8221; method. Example:</p>
<div><span>if</span> (Request.IsMvcAjaxRequest())<br />
{<span><br />
   // Do async things here&#8230;</span><br />
}</div>
</div>
<p>And then to use it in our ActionMethod, here&#8217;s what we would do:</p>
<div><span>public</span> ActionResult List()<br />
{<br />
   <span>this</span>.ViewData.Model = <span>this</span>.GetNewsItems();</p>
<p>   <span>if</span> (<span>this</span>.Request.IsMvcAjaxRequest() == <span>false</span>)<br />
   {<span><br />
       // Non AJAX requests see the entire ViewPage.</span><br />
       <span>return</span> View();<br />
   }<br />
   <span>else</span><br />
   {<span><br />
       // AJAX requests just <span>get</span> a trimmed down UserControl.</span><br />
       <span>return</span> View(<span>&#8220;List_AJAX&#8221;</span>);<br />
   }<br />
}</div>
<p>Notice that we don&#8217;t have to duplicate any logic for getting the latest news. The only bit of custom logic that is needed is to determine which view to send. This can be done for form posts too (as you can see in the screen shots below or by downloading the project at the end of the article). Here&#8217;s what the results of the above will look like for an AJAX enabled, and a non-AJAX enabled browser:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 1" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=23d3731b-07bd-46d6-b74e-c215b65f696c" alt="ASP.NET MVC - AJAX application that gracefully degrades part 1" /></p>
<p>After clicking on the &#8220;view news&#8221; link with AJAX enabled:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 2" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=1f6199f8-129e-485c-a08a-8ed830114b55" alt="ASP.NET MVC - AJAX application that gracefully degrades part 2" /></p>
<p>After clicking &#8220;view news&#8221; with JavaScript (and therefore AJAX) disabled:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 3" src="http://www.comteken.com/wp-admin/Articles/UserImage.aspx?ImageID=cd8aa437-45c4-4f66-b84e-e5cd44256583" alt="ASP.NET MVC - AJAX application that gracefully degrades part 3" /></p>
<p>If you notice the URLs in the second and third screen shots, you&#8217;ll see that with AJAX disabled the browser simply navigated to where the link was pointing. Thus, a graceful degradation was achieved.</p>
<h3>Conclusion</h3>
<p>With little effort, we achieved an AJAX enabled ASP.NET MVC site, and cleanly/gracefully allowed non-AJAX users to enjoy the same benefits of our demo app. The project was written in Visual Studio 2008 SP1 with the ASP.NET MVC Beta 1 bits installed. Because the DLL&#8217;s are bin-deployed, you should be able to download and run the project even if you don&#8217;t have the MVC beta install. Check it out for yourself: <a href="http://www.comteken.com/wp-admin/Articles/UserFile.aspx?FileID=997f0db8-ac1d-4405-a336-eb0b8416d12e">SingingEels_MvcAjaxMashup.zip</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;bodytext=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;notes=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;t=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;annotation=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;Title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;headline=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;bm_description=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;t=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;h=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;story_title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade-2%2F&amp;submitHeadline=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;submitSummary=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Custom View Engine in ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc-2/</link>
		<comments>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:08:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=49</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc-2/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>ASP.NET is an awesome web development platform that many developers wouldn&#8217;t ever want to part with. However, if given the option, there are definitely parts of the package that you may want to swap out. This article will show you how easy it can be to break away from the &#8220;inline-ASP&#8221; or &#8220;Web Forms&#8221; world, [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET is an awesome web development platform that many developers wouldn&#8217;t ever want to part with. However, if given the option, there are definitely parts of the package that you may want to swap out. This article will show you how easy it can be to break away from the &#8220;inline-ASP&#8221; or &#8220;Web Forms&#8221; world, and delve into your own custom ViewEngine.</p>
<p>It&#8217;s true that much of the ASP.NET framework has been pluggable for a while (example: Membership and Profiles). Also, we have always had a choice of server-side language from the start (example: C# or VB). But only recently with the birth of ASP.NET MVC do we have a clean and straight forward solution to swap out the &#8220;ASP&#8221; language for our own.</p>
<p>By the end of this article, we will have made our own language (called &#8220;HoTMeaT&#8221;), we will have registered our custom ViewEngine and we will notice just how easy it is to swap out this seemingly &#8216;fixed&#8217; area of ASP.NET. At the end of the article, you can download the entire demo project &#8211; including the source code for the language.</p>
<div>
<p>It&#8217;s important to note that we have always had the option of writing a custom HttpHandler. But that meant that we have to take over all aspects of the request life-cycle. But with ASP.NET MVC&#8217;s custom &#8220;ViewEngine&#8221; capabilities, we are *ONLY* tapping into the slice of the cycle that takes a file and outputs HTML. The &#8220;controller&#8221; will still function the same; security will still be enforced, etc.</p></div>
<h3>A Sneak Peek Into the Language</h3>
<p>Before we get into a &#8220;step-by-step&#8221; of how to write our own ViewEngine, I want to show you how amazing this new &#8220;HoTMeaT&#8221; language is going to look. Since I&#8217;m currently on a WPF kick, I&#8217;m going to copy some of that great functionality by taking &#8220;binding&#8221; to the next level.</p>
<p>Here&#8217;s the code for the Customers/List page. We won&#8217;t need to do anything different in our code, because really, the &#8220;Controller&#8221; doesn&#8217;t care about how &#8220;View&#8221; will look. It&#8217;s only responsible for getting the data.</p>
<div><span>public</span> ActionResult List()<br />
{<br />
   ViewData[<span>"People"</span>] = <span>new</span> Person[]<br />
   {<br />
       <span>new</span> Person{FirstName=<span>&#8220;Timothy&#8221;</span>, LastName=<span>&#8220;Khouri&#8221;</span>},<br />
       <span>new</span> Person{FirstName=<span>&#8220;Jonathan&#8221;</span>, LastName=<span>&#8220;Carter&#8221;</span>},<br />
       <span>new</span> Person{FirstName=<span>&#8220;Travis&#8221;</span>, LastName=<span>&#8220;Sheppard&#8221;</span>},<br />
   };</p>
<p>   <span>return</span> View();<br />
}</div>
<p>Now, how would you display a list of those people in your own custom HTML template? Well, if you were using WebForms (traditional ASP.NET), you would probably use a Repeater and then do data binding in the code behind. If you are using ASP.NET MVC, you would probably just write a for-loop with sloppy inline ASP. But, with HoTMeaT&#8230;</p>
<div>&lt;<span>ul</span>&gt;<br />
   &lt;<span>listView</span> <span>source</span><span>=&#8221;{ViewData People}&#8221;</span>&gt;<br />
       &lt;<span>itemTemplate</span>&gt;<br />
           &lt;<span>li</span>&gt;{Binding LastName}, {Binding FirstName}&lt;<span>/li</span>&gt;<br />
       &lt;<span>/itemTemplate</span>&gt;<br />
   &lt;<span>/listView</span>&gt;<br />
&lt;<span>/ul</span>&gt;</div>
<p>And here&#8217;s the HTML that it generates:</p>
<div>&lt;<span>ul</span>&gt;<br />
   &lt;<span>li</span>&gt;Khouri, Timothy&lt;<span>/li</span>&gt;<br />
   &lt;<span>li</span>&gt;Carter, Jonathan&lt;<span>/li</span>&gt;<br />
   &lt;<span>li</span>&gt;Sheppard, Travis&lt;<span>/li</span>&gt;<br />
&lt;<span>/ul</span>&gt;</div>
<p>Now that&#8217;s pretty cool. But before we go on, there is a myth that needs to be dispelled. I&#8217;ve heard multiple people say that if you use / build a custom ViewEngine that you&#8217;ll suffer some great performance hit. That is completely not true. The HoTMeaT view engine (which parses the HTML for custom tags, and performs data binding at runtime for each request) runs at 0.00015 seconds. That means that it can handle hundreds of requests per second from my laptop. So no, there are no performance concerns (unless of course you&#8217;re a bad programmer).</p>
<h3>Creating and Registering your Custom ViewEngine</h3>
<p>ASP.NET MVC was built to allow developers to change certain parts of the pipe-line in a similar way. For example, creating and registering a ViewEngine is much like creating your own <a rel="nofollow" href="http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx">custom model binder</a> and then registering it at runtime. We&#8217;ll create a class that inherits from &#8220;IViewEngine&#8221;. Then (just like with registering a ModelBinder) we&#8217;ll register our custom ViewEngine in the Global.asax file. Here&#8217;s the code:</p>
<div><span>protected</span> <span>void</span> Application_Start()<br />
{<span><br />
   //&#8230; other things up here.</span><span></p>
<p>   // I want to REMOVE the ASP.NET ViewEngine&#8230;</span><br />
   ViewEngines.Engines.Clear();<span></p>
<p>   // and then <span>add</span> my own <img src='http://www.comteken.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span><br />
   ViewEngines.Engines.Add(<span>new</span> HoTMeaTViewEngine());<br />
}</div>
<p>Inside of our ViewEngine class we only have to provide the functionality for two methods &#8211; CreateView and CreatePartialView. Those methods will return an instance of a class that implements &#8220;IView&#8221;. The View class has one method in it &#8211; Render.</p>
<div>
<p>While it&#8217;s true that your custom ViewEngine only needs to implement the IViewEngine interface, I went a small step further and implemented the &#8220;VirtualPathProviderViewEngine&#8221; class. That class implements IViewEngine, but it also provides a little extra functionality that helps us tell MVC where to look for our View files.</p></div>
<p>Here&#8217;s the code for our ViewEngine:</p>
<div><span>public</span> <span>class</span> HoTMeaTViewEngine : VirtualPathProviderViewEngine<br />
{<br />
   <span>public</span> HoTMeaTViewEngine()<br />
   {<span><br />
       // This <span>is</span> <span>where</span> we tell MVC <span>where</span> to look <span>for</span> our files. This says</span><span><br />
       // to look <span>for</span> a file at <span>&#8220;Views/Controller/Action.html&#8221;</span></span><br />
       <span>base</span>.ViewLocationFormats = <span>new</span> <span>string</span>[] { <span>&#8220;~/Views/{1}/{0}.html&#8221;</span> };<br />
       <br />
       <span>base</span>.PartialViewLocationFormats = <span>base</span>.ViewLocationFormats;<br />
   }</p>
<p>   <span>protected</span> <span>override</span> IView CreateView(ControllerContext context, <span>string</span> viewPath, <span>string</span> masterPath)<br />
   {<br />
       <span>return</span> <span>new</span> HoTMeaTView(viewPath, masterPath);<br />
   }</p>
<p>   <span>protected</span> <span>override</span> IView CreatePartialView(ControllerContext context, <span>string</span> partialPath)<br />
   {<br />
       <span>return</span> <span>new</span> HoTMeaTView(partialPath, <span>&#8220;&#8221;</span>);<br />
   }<br />
}</div>
<h3>How Do I Build my View?</h3>
<p>So now that we&#8217;ve created our ViewEngine and registered it in the Global.asax, we have to actually do something in our View class. As was mentioned above, the only method that we have to implement is &#8220;Render&#8221;, which passes in the viewContext (which gives us the Controller, ViewData, RouteData, etc) and a TextWriter for us to render to. So, if you wanted to make a *very* basic view engine that simply shells out a static file, you could do this:</p>
<div><span>public</span> <span>class</span> MyView : IView<br />
{<br />
   <span>public</span> HoTMeaTView(<span>string</span> viewPath)<br />
   {<br />
       <span>this</span>.ViewPath = viewPath;<br />
   }</p>
<p>   <span>public</span> <span>string</span> ViewPath { <span>get</span>; <span>private</span> <span>set</span>; }</p>
<p>   <span>public</span> <span>void</span> Render(ViewContext viewContext, TextWriter writer)<br />
   {<br />
       <span>string</span> filePath = viewContext.HttpContext.Server.MapPath(<span>this</span>.ViewPath);</p>
<p>       <span>string</span> fileContents = File.ReadAllText(filePath);</p>
<p>       writer.Write(File.ReadAllText(fileContents);<br />
   }<br />
}</p></div>
<p>And that&#8217;s really it! We&#8217;ve just made our own ViewEngine which creates a simple View that writes out the contents of a file. Now, you may be wondering what happened to my great new &#8220;HoTMeaT&#8221; language where we could do all that great data-binding right in our HTML. If you want to see all that code, then you can download the source project and take a look at the code yourself. That code is intentionally left that so as not to detract from the main point of the article. So here&#8217;s the full demo project with all of the code including the &#8220;HotMeatViewEngine&#8221;: <a href="http://www.comteken.com/wp-admin/Articles/UserFile.aspx?FileID=d8db8937-47a3-49f7-9036-906ee71d07a4">SingingEels_CustomMvcViewEngine.zip</a></p>
<h3>So What Is &#8220;HoT MeaT&#8221;?</h3>
<p>Back in college I had a very &#8220;build it yourself to learn it&#8221; mentality (and really I still do today). So, I wanted to understand Sockets and client/server programming, so I made my own &#8220;web server&#8221;. All it did was shell out static HTML files. Then for some reason, my friend and I decided that we would make our own language: HTMT (&#8220;Hyper Text Meta Transformation&#8221; &#8211; aka &#8220;HoT MeaT&#8221;). I think we were bored in C++ 101, and bordom leads to invention! We got to the point of displaying the time on the server and basically stopped there. So that&#8217;s the story behind the name.</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;bodytext=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;notes=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;t=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;annotation=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;Title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;headline=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;bm_description=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;t=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;h=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;story_title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc-2%2F&amp;submitHeadline=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;submitSummary=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Model Binders in ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc/</link>
		<comments>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:25:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=19</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc/"><img align="left" hspace="5" width="100" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=ccf389f4-da2a-4f13-9870-0cef371ec5aa" class="alignleft wp-post-image tfe" alt="MVC Model Binders Demo Screen Shot" title="MVC Model Binders Demo Screen Shot" /></a>Hot off the presses, and new to ASP.NET MVC (Preview 5) is an awesome capability that (in my opinion) revolutionizes the way we design web applications. This feature is being touted (by me) as &#8220;the ViewState for MVC&#8221;. First, the Challenge A common challenge of web applications is passing complex types and (more difficultly) stateful [...]]]></description>
			<content:encoded><![CDATA[<p>Hot off the presses, and new to ASP.NET MVC (Preview 5) is an awesome capability that (in my opinion) revolutionizes the way we design web applications. This feature is being touted (by me) as &#8220;the ViewState for MVC&#8221;.</p>
<h3>First, the Challenge</h3>
<p>A common challenge of web applications is passing complex types and (more difficultly) stateful objects from one page to another. This difficulty is due to the disconnected nature of the web. There have been many attempts to solve this problem (sessions, ViewState, etc), but ASP.NET MVC&#8217;s new &#8220;Model Binders&#8221; functionality definitely takes the gold medal for being the cleanest, simplest and most powerful.</p>
<p>We&#8217;re going to build a couple of simple MVC pages, one that display a list of customers and the other displays their address details. You can download the solution at the end of the article (compiled for Visual Studio 2008 SP1 / ASP.NET MVC Preview 5).</p>
<h3>What Are the Benefits?</h3>
<p>ASP.NET MVC Model Binders give you the following benefits:</p>
<ul>
<li>Complete control over the deserialization of complex types passed into your Action methods. This in itself has many sub-benefits: less bloat in your HTML and in passing objects back to the server, cleaner URL&#8217;s, etc.</li>
<li>Unified and testable code. This feature really holds to the &#8220;Single Responsibility&#8221; (or separation of concerns) principle. This one place is where you will handle restoring your objects from the memento you come up with.</li>
</ul>
<div>
<p>The idea of using a ModelBinder to pass tokenized objects that live in your data model is not the typical usage. You could use them to define how other complex objects that aren&#8217;t serialized into the query string get passed into your methods, but purpose of this article is to show a more extreme example to demonstrate it&#8217;s power and capabilities.</p>
<p>A more common usage for ModelBinders would be to build your complex object from a form post. Example:</p>
<div><span>public</span> <span>override</span> <span>object</span> GetValue(ControllerContext ctx,<br />
<span>string</span> modelName, Type modelType, ModelStateDictionary state)<br />
{<br />
Customer customer = <span>new</span> Customer();</p>
<p>customer.FirstName = ctx.HttpContext.Request[<span>"FirstName"</span>];</p>
<p>customer.LastName = ctx.HttpContext.Request[<span>"LastName"</span>];<span></p>
<p>// &#8230; other properties &#8230;</span></p>
<p><span>return</span> customer;<br />
}</div>
</div>
<p>By the end of this article, we&#8217;ll see how easy it is pass complex &#8220;Customer&#8221; objects around our application and we&#8217;ll talk about how to create unit tests to ensure the quality of our methods. Our customer information will be stored in a SQL database, and the customer class will be generated by the LINQ to SQL as a concrete representation of a record in our database.</p>
<p>Before we dive into our application, let&#8217;s take a look at a snippet of code and see how MVC model binders make a difference.</p>
<h3>MVC ModelBinder vs. Traditional ASP.NET</h3>
<p>To demonstrate this functionality simply, consider the following scenario. You&#8217;re looking at the &#8220;List&#8221; page for your users. The list of users is in an HTML &lt;table&gt;, and the first column is a link to that user&#8217;s &#8220;Edit&#8221; page.</p>
<p>Let&#8217;s assume our link will produce the following URL: &#8220;/Users/Edit.aspx?UserID=123&#8243;. In traditional ASP.NET, we would probably have the following code at the top of our page.</p>
<div><span>private</span> <span>void</span> Page_Load(<span>object</span> sender, EventArgs e)<br />
{<br />
<span>int</span> userID = 0;</p>
<p><span>if</span> (<span>int</span>.TryParse(Request[<span>"UserID"</span>], <span>out</span> userID) == <span>false</span>)<br />
{<span><br />
// User ID wasn&#8217;t supplied, so redirect <span>out</span>.</span><br />
}</p>
<p>User currentUser = GetUserFromDatabase(userID);</p>
<p><span>if</span> (currentUser == <span>null</span>)<br />
{<span><br />
// Bad User ID, so redirect <span>out</span>.</span><br />
}<span></p>
<p>// &#8230;</span><br />
}</div>
<p>This may not seem that bad, but if you think about it, we&#8217;ll have to re-write this code in every page that we want to pass a user object to and for every parameter that we need to have passed to the page. This approach isn&#8217;t very object oriented, and you&#8217;re constantly reminded of the fact that you&#8217;re just grabbing data out of the query string.</p>
<p>Now, notice the difference between what you have to do in traditional ASP.NET, and ASP.NET MVC (thanks to Model Binders):</p>
<div><span>public</span> ActionResult Edit(User currentUser)<br />
{<br />
<span>if</span> (currentUser == <span>null</span>)<br />
{<span><br />
// User not supplied, so redirect <span>out</span>.</span><br />
}<span></p>
<p>// &#8230;</span><br />
}</div>
<p>Notice how much simpler that was? OK, so you might be wondering &#8220;where is the code?&#8221;&#8230; &#8220;How is the &#8216;currentUser&#8217; parameter being passed in?&#8221; Now it&#8217;s time to see the magic.</p>
<h3>Creating and Registering Model Binders</h3>
<p>Because ASP.NET MVC is designed to make things simple and easy to use, creating and registering your own Model Binders is a quick task. There are really only two things you have to do to handle the &#8216;serialization / deserialization&#8217; part, and there is one simple line of code to register your custom model binder with the MVC framework. Let&#8217;s break it down here.</p>
<p>First of all, we need to decide how we want to &#8216;tokenize&#8217; our object (meaning, what string representation do we want to make so that we can get our object back later). You currently have three options here, but I would like to see a fourth one by the time MVC goes live (I&#8217;ll explain in a minute).</p>
<p>Essentially, the three choices you have all boil down to the same thing:</p>
<ul>
<li>Make your class implement the IConvertible interface, and provide your code for how it should &#8216;convert&#8217; to a string.</li>
<li>Make your class implement the IFormattable interface, and do the same as the above.</li>
<li>Or, simply override &#8220;ToString&#8221; and put your logic there.</li>
</ul>
<p>I&#8217;m going to use this third approach because it&#8217;s simple, but you certainly could go with one of the above. In some cases, it would be best to use the first choice (the IConvertible). You may want to choose this so that you can free up your &#8220;ToString&#8221; method to provide a user-friendly looking string representation of your object, and then use the ConvertTo method to provide your memento.</p>
<p>Example: &#8216;ToString()&#8217; might return &#8220;Khouri, Timothy&#8221;, and &#8216;ConvertTo()&#8217; might return &#8216;ID:1234&#8242;.</p>
<div>
<p>I mentioned that I would like to see a fourth option soon, and this one I think is the most powerful. As you will see below, ModelBinders give you the option to &#8220;restore&#8221; your object from the string token that is provided. I would like to also see the code that converts *to* that token in this same model binder.</p>
<p>There are many great reasons for this, but the most important I can think of is to give you (the developer) the ability to convert classes that you didn&#8217;t write into whatever format you want. i.e.: DateTime to serialize as &#8220;yyyyMMdd&#8221;.</p></div>
<p>Because my &#8220;Customer&#8221; class is being auto-generated by LINQ-to-SQL, I&#8217;m going to create a class file that extends it a little to provide my own &#8220;ToString&#8221; method. This is not an &#8220;extension method&#8221;, but rather just a partial class file. Here&#8217;s my code:</p>
<div><span>public</span> <span>partial</span> <span>class</span> Customer<br />
{<br />
<span>public</span> <span>override</span> <span>string</span> ToString()<br />
{<span><br />
// I could just <span>return</span> <span>&#8220;ID.ToString()&#8221;</span>, but I</span><span><br />
// want to be a little silly to prove that I have full</span><span><br />
// control over how my memento <span>is</span> going to look <img src='http://www.comteken.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
<p><span>return</span> Convert.ToBase64String(<br />
BitConverter.GetBytes(<span>this</span>.ID)<br />
).Replace(<span>&#8220;=&#8221;</span>, <span>&#8220;_&#8221;</span>);<br />
}<br />
}</div>
<p>Basically, this will convert any instance of a Customer class to a string that looks something like &#8220;CwAAB0__&#8221;. Now, I need to make my ModelBinder that will take that string, convert it back to the ID of the Customer, and then re-create the Customer object (which in this case, will be a simple task for LINQ-to-SQL).</p>
<p>As was mentioned before, creating our own custom ModelBinder is very simple. We&#8217;re going to inherit from &#8220;DefaultModelBinder&#8221;, and then override the &#8220;ConvertType&#8221; method. The &#8216;value&#8217; object that gets passed in could be a string, or an array of strings, so I&#8217;ll have to check for that. Here&#8217;s the code:</p>
<div><span>public</span> <span>class</span> MyCustomerBinder : DefaultModelBinder<br />
{<br />
<span>protected</span> <span>override</span> <span>object</span> ConvertType(CultureInfo culture, <span>object</span> value, Type destinationType)<br />
{<span><br />
// This <span>class</span> <span>is</span> only meant to convert Customer objects.</span><br />
<span>if</span> (destinationType != <span>typeof</span>(Customer))<br />
{<br />
<span>return</span> <span>base</span>.ConvertType(culture, value, destinationType);<br />
}<span></p>
<p>// Get the ID that <span>is</span> being passed <span>in</span>.</span><br />
<span>string</span> customerIDString = value <span>as</span> <span>string</span>;</p>
<p><span>if</span> (customerIDString == <span>null</span> &amp;&amp; value <span>is</span> <span>string</span>[])<br />
{<br />
customerIDString = ((<span>string</span>[])value)[0];<br />
}</p>
<p>customerIDString = customerIDString.Replace(<span>&#8220;_&#8221;</span>, <span>&#8220;=&#8221;</span>);</p>
<p><span>int</span> customerID = BitConverter.ToInt32(Convert.FromBase64String(customerIDString), 0);<span></p>
<p>// Grab the customer <span>from</span> the database.</span><br />
<span>return</span> MyDataAccessLayer.GetCustomerByID(customerID);<br />
}<br />
}</div>
<p>That&#8217;s it! We&#8217;ve now created a ModelBinder that will tell MVC how to convert that token back into a real, tangible Customer object. Next we&#8217;ll see how to register and use our custom parameter converting model binder.</p>
<p>In our global application file (Global.asax), we are already registering our routing information for MVC (which tells the framework where to look for our Controllers and Actions). Now we&#8217;re going to add this simple line of code that tells the MVC framework to how to reconstitute our Customer class. Here&#8217;s the code:</p>
<div><span>// Register our ModelBinder.</span><br />
ModelBinders.Binders.Add(<span>typeof</span>(Customer),<br />
<span>new</span> MyCustomerBinder());</div>
<h3>Seeing the Results</h3>
<p>There is no better way to fully appreciate what is happening here other than seeing the result for yourself. In my &#8216;list customers&#8217; page, I&#8217;m building the link to the details page in a very object-oriented way:</p>
<div><span>&lt;%</span><br />
<span>foreach</span> (Customer customer <span>in</span> <span>this</span>.ViewData.Model)<br />
{<br />
<span>this</span>.Writer.Write(<span>&#8220;&lt;li&gt;&#8221;</span>);<span></p>
<p>// Create a link that poitns to the <span>&#8220;Details&#8221;</span> method, passing</span><span><br />
// <span>in</span> the *customer* <span>object</span>. The link should display the full</span><span><br />
// name of the customer.</span><br />
<span>this</span>.Writer.Write(<span>this</span>.Html.ActionLink&lt;HomeController&gt;(<br />
c =&gt; c.Details(customer), customer.FullName));</p>
<p><span>this</span>.Writer.Write(<span>&#8220;&lt;/li&gt;&#8221;</span>);<br />
}<br />
<span>%&gt;</span></div>
<p>Here&#8217;s is what our page will look like:</p>
<p><img title="MVC Model Binders Demo Screen Shot" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=ccf389f4-da2a-4f13-9870-0cef371ec5aa" alt="MVC Model Binders Demo Screen Shot" /></p>
<p>And here is the details page. Notice the URL. If you scroll up you&#8217;ll see that I never told MVC to call the parameter &#8220;targetCustomer&#8221;, but MVC automatically figured that out because that&#8217;s what the parameter is named in my &#8220;Details&#8221; method.</p>
<p><img title="MVC Model Binders Demo Screen Shot (Details Page)" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=c2cebfd3-b51d-4219-9c2f-34cf65960526" alt="MVC Model Binders Demo Screen Shot (Details Page)" /></p>
<h3>Testing Your Converter</h3>
<p>As I stated above, I wanted to mention why this approach is good in terms of testability. Basically, because this bit of functionality is so small and specific, it is fully testable. You can use your favorite &#8216;mock&#8217; framework to create a FakeHttp request with your token in the query string, and see if it correctly creates the Customer class.</p>
<p>As a side note: don&#8217;t forget that my model binder is hitting the data access layer, so if you are going to create unit tests you&#8217;ll want to also mock the DAL as well.</p>
<p>And now, here is the solution (compiled for Visual Studio 2008 SP1, ASP.NET MVC Preview 5): <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=9b82536f-717c-42cf-8397-b7fae5380985">SingingEels_MVC_ModelBinders.zip</a></p>
<div><span><br />
</span></div>
<div><span><br />
</span></div>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;bodytext=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;notes=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;annotation=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;Title=Model%20Binders%20in%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Model%20Binders%20in%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;headline=Model%20Binders%20in%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;bm_description=Model%20Binders%20in%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Model%20Binders%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;h=Model%20Binders%20in%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Model%20Binders%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;story_title=Model%20Binders%20in%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Model%20Binders%20in%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc%2F&amp;submitHeadline=Model%20Binders%20in%20ASP.NET%20MVC&amp;submitSummary=Hot%20off%20the%20presses%2C%20and%20new%20to%20ASP.NET%20MVC%20%28Preview%205%29%20is%20an%20awesome%20capability%20that%20%28in%20my%20opinion%29%20revolutionizes%20the%20way%20we%20design%20web%20applications.%20This%20feature%20is%20being%20touted%20%28by%20me%29%20as%20%22the%20ViewState%20for%20MVC%22.%0D%0AFirst%2C%20the%20Challenge%0D%0AA%20common%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Driven Development with ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc/</link>
		<comments>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:12:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=16</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc/"><img align="left" hspace="5" width="100" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=b62d323a-435a-4d72-8757-cb1afeafefbc" class="alignleft wp-post-image tfe" alt="New ASP.NET MVC Preview 5 project in Visual Studio 2008" title="New ASP.NET MVC Preview 5 project in Visual Studio 2008" /></a>One of the biggest benefits of MVC is it&#8217;s direct link to Test Driven Development. Because of some of the new features of ASP.NET MVC Preview 5 (ModelBinders in particular), testing your Action methods is even easier. This article will demonstrate how easy it is to ensure the quality of your MVC app with unit [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest benefits of MVC is it&#8217;s direct link to Test Driven Development. Because of some of the new features of ASP.NET MVC Preview 5 (ModelBinders in particular), testing your Action methods is even easier. This article will demonstrate how easy it is to ensure the quality of your MVC app with unit tests.</p>
<p>For those of you who are not familiar with Test Driven Development (TDD) or it&#8217;s benefits, consider the following scenario. Let&#8217;s assume that you have been contracted to build a site that deals with customer management. One of the client&#8217;s business rules is that customers must have at least a first name, last name and a zip code. This is a fairly straight forward request that will help us easily get into TDD.</p>
<h3>Creating MVC Apps With Unit Tests</h3>
<p>To prove that MVC is meant for testing, the Visual Studio template for a new ASP.NET MVC (Preview 5) project will automatically ask if you want to include a Unit Test project as well.</p>
<p>The default Test Project that comes with the ASP.NET MVC Visual Studio template is pretty basic, and includes a test class for each of the default Controller&#8217;s that come with your MVC app.</p>
<p>So, the steps to start our project are very simple:</p>
<ol>
<li>File -&gt; New -&gt; Project</li>
<li>ASP.NET MVC Web Application</li>
<li>Yes, create a unit test project.</li>
</ol>
<p><img title="New ASP.NET MVC Preview 5 project in Visual Studio 2008" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=b62d323a-435a-4d72-8757-cb1afeafefbc" alt="New ASP.NET MVC Preview 5 project in Visual Studio 2008" /> <img title="Create Unit Test Project in Visual Studio 2008" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=f9fb574e-e466-4c27-8b3d-76f914db87d0" alt="Create Unit Test Project in Visual Studio 2008" /></p>
<h3>Business Rules as Tests</h3>
<p>So, after learning what our business rules are, our next step in TDD is to express those business rules in the form of unit tests. To do this, we&#8217;ll add a class to our Test Project that will handle testing all things &#8220;Customers&#8221; related. We&#8217;ll need to decorate the class with the &#8220;TestClass&#8221; attribute, and each method with the &#8220;TestMethod&#8221; attribute. Our &#8220;customers test class&#8221; should look like this:</p>
<div><span>// The name of the <span>class</span> doesn&#8217;t matter, but <span>this</span> name</span><span><br />
// makes sense to me.</span><br />
[TestClass]<br />
<span>public</span> <span>class</span> TestCustomersController<br />
{<span><br />
// Also, the name of the method doesn&#8217;t matter,</span><span><br />
// but again, <span>this</span> name makes sense <span>for</span> our goal.</span><br />
[TestMethod]<br />
<span>public</span> <span>void</span> EnsureRequiredFields()<br />
{<span><br />
// Our test code will go here.</span><br />
}<br />
}</div>
<p>Now that we have a stub for our tests, we need to do a *little* bit of implementing our code in our CustomersController class in our ASP.NET MVC app. All we are going to do is create the method stub that is responsible for creating new customers. Once we do that, we can come back and finish our test method.</p>
<p>I mentioned earlier in the article that using a ModelBinder will make writing tests easier, and that may seem confusing at first. ModelBinders are run by the ASP.NET MVC framework, and will not be used by our Test Project, so how can they help in writing unit tests?</p>
<p>The answer is simple: Object Oriented Programming. Basically, because ModelBinders allow me to write my ASP.NET MVC Action Methods with complex objects as parameters, I can test against them in a much more OO fashion. Example:</p>
<div><span>// Not a very OO way of doing things, and also not</span><span><br />
// fun to test.</span><br />
<span>public</span> ActionResult Create()<br />
{<span><br />
// Pull values <span>from</span> HttpRequest?</span><br />
<span>if</span> (<span>string</span>.IsNullOrEmpty(Request[<span>"firstName"</span>]))<br />
{<span><br />
// <span>return</span> some sort of validation error.</span><br />
}<br />
}<span></p>
<p>// Whereas <span>this</span> <span>is</span> a very OO way of doing things, and</span><span><br />
// <span>is</span> also very easy to write tests against!</span><br />
<span>public</span> ActionResult Create(Customer newCustomer)<br />
{<br />
<span>if</span> (<span>string</span>.IsNullOrEmpty(newCustomer.FirstName))<br />
{<span><br />
// <span>return</span> some sort of validation error.</span><br />
}<br />
}</div>
<p>So now we are ready to write our tests, but first we should mention a little &#8220;gotcha&#8221; that needs to be addressed. Because I only want to test my business rules, and not the code that ultimately writes the customer record to the database, I need to prevent my code from actually running the &#8220;store in database&#8221; part of my code. This is known as &#8220;mocking&#8221;.</p>
<div>
<p>While there are many great mock frameworks out there (Moq, NMock, Rhino.Mocks, and many more), I&#8217;m using Typemock Isolator for this article. Why? &#8211; Because.</p></div>
<h3>Testing One Step at a Time</h3>
<p>There are three pieces to our business rule that we are going to test individually: Required first name, last name and zip. To do this, we&#8217;re going to create an instance of our CustomersController, and call Create method. We&#8217;re going to pass in a customer object that *should fail validation*, and then we&#8217;ll check to make sure that, in fact, the Create method did fail.</p>
<p>This may seem odd (to hope for failure), but again remember that we want to test to make sure that validation is working, so we have to pass in bad data and look for the validation to come back.</p>
<p>Here&#8217;s our code that we&#8217;ll put inside of our &#8220;EnsureRequiredFields&#8221; method mentioned earlier in the article:</p>
<div>Customer newCustomer;</p>
<p>CustomersController controller = <span>new</span> CustomersController();<span></p>
<p>// We don&#8217;t want to actually hit the database, so we&#8217;ll just tell the test framework to ignore <span>this</span> method.</span><br />
MockManager.Mock(<span>typeof</span>(DataStore)).ExpectAlways(<span>&#8220;InsertCustomer&#8221;</span>);</p>
<p>{ // Test <span>for</span> required <span>&#8220;FirstName&#8221;</span>.<br />
controller.ViewData.ModelState.Clear();</p>
<p>newCustomer = <span>new</span> Customer<br />
{<br />
FirstName = <span>&#8220;&#8221;</span>,<br />
LastName = <span>&#8220;Smith&#8221;</span>,<br />
Zip = <span>&#8220;34275&#8243;</span>,<br />
};</p>
<p>controller.Create(newCustomer);<span></p>
<p>// Make sure that our validation found the error!</span><br />
Assert.IsTrue(controller.ViewData.ModelState.Count == 1, <span>&#8220;FirstName must be required.&#8221;</span>);<br />
}<span></p>
<p>// then we&#8217;ll test <span>for</span> LastName, and then Zip <span>in</span> the same way.</span></div>
<p>Now, when we run our tests in Visual Studio we will fail until we code the appropriate validation in our Create method. Here&#8217;s what that will look like:</p>
<p><img title="Failed MVC unit test requiring FirstName" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=6c169f9b-d180-4cd8-a018-7ce3697aeba9" alt="Failed MVC unit test requiring FirstName" /></p>
<p>Now you keep coding your app until you pass that test (which in this case should be pretty easy). So, once we add this code to our Create method, we&#8217;ll be good to go:</p>
<div><span>if</span> (<span>string</span>.IsNullOrEmpty(newCustomer.FirstName))<br />
{<br />
<span>this</span>.ViewData.ModelState.AddModelError(<span>&#8220;FirstName&#8221;</span>, <span>&#8220;&#8221;</span>, <span>&#8220;&#8216;FirstName&#8217; <span>is</span> a required field.&#8221;</span>);</p>
<p>isValid = <span>false</span>;<br />
}<span></p>
<p>// also test <span>for</span> LastName and Zip&#8230;</span></p>
<p><span>if</span> (isValid == <span>false</span>)<br />
{<br />
<span>return</span> View();<br />
}</div>
<h3>Conclusion</h3>
<p>Hopefully this article has been able to demonstrate how easy it is to start down the path of TDD with the ASP.NET MVC framework. The demo project for this article can be downloaded here: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=61aa06f8-fc3a-4009-ab1c-a68a27a4e2fe">MvcDemo_Testing_CreateCustomer.zip</a>.</p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;bodytext=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;notes=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;t=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;annotation=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;Title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;headline=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;bm_description=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;t=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;h=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;story_title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;title=Test%20Driven%20Development%20with%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Test%20Driven%20Development%20with%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Ftest-driven-development-with-asp-net-mvc%2F&amp;submitHeadline=Test%20Driven%20Development%20with%20ASP.NET%20MVC&amp;submitSummary=One%20of%20the%20biggest%20benefits%20of%20MVC%20is%20it%27s%20direct%20link%20to%20Test%20Driven%20Development.%20Because%20of%20some%20of%20the%20new%20features%20of%20ASP.NET%20MVC%20Preview%205%20%28ModelBinders%20in%20particular%29%2C%20testing%20your%20Action%20methods%20is%20even%20easier.%20This%20article%20will%20demonstrate%20ho&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/test-driven-development-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Model Binders in ASP.NET MVC &#8211; Part 2</title>
		<link>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-part-2/</link>
		<comments>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-part-2/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 14:53:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=14</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-part-2/"><img align="left" hspace="5" width="100" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=44f56526-3db4-40db-acd7-5dfe1f4c0743" class="alignleft wp-post-image tfe" alt="Dynamic address fields in an ASP.NET MVC application" title="Dynamic address fields in an ASP.NET MVC application" /></a>The previous article on ModelBinders gave a basic introduction, showed a few code samples, and showed a creative usage of ModelBinders. However, some questions arose, some claims were challenged (particularly about comparing ModelBinders to the WebForms ViewState) and some controversial code needs further flushing out. This article will do just that. To begin with, we [...]]]></description>
			<content:encoded><![CDATA[<p>The previous article on ModelBinders gave a basic introduction, showed a few code samples, and showed a creative usage of ModelBinders. However, some questions arose, some claims were challenged (particularly about comparing ModelBinders to the WebForms ViewState) and some controversial code needs further flushing out. This article will do just that.</p>
<p>To begin with, we need to define the list of items that we are going to resolve in this article, as well as the list of rules/methods we are going to use to determine the worth and quality of these items. By the end of this article we will resolve:</p>
<ol>
<li>Does the use of ModelBinders hinder the URL in anyway? Can we still achieve &#8220;RESTful&#8221; URLs if we use ModelBinders?</li>
<li>In what ways do ASP.NET MVC ModelBinders compare to the ViewState feature of ASP.NET WebForms?</li>
<li>We have demonstrated that we *can* use ModelBinders to recreate stateful objects from a database, but *should* we ever do that? Are there any benefits? Are we violating cosmic laws?</li>
</ol>
<h3>Examining the Issues</h3>
<p>The first item may seem odd as ModelBinders have nothing to do with your URL, but for some reason multiple comments were made (on a previous article) expressing concerns for such. The second question is good to examine because MVC doesn&#8217;t have the ability to use the ViewState, so we&#8217;ll see how to cover some web-development-challenges with ModelBinders that we might have solved with the ViewState in the past. The final topic to cover is probably the most important, and is definitely the most controversial. As such, we will use the following rules to determine a good solution:</p>
<ol>
<li>Does the solution fit one or both of the main pillars of the MVC design pattern &#8211; Testability and Separating Concerns?</li>
<li>Does the solution provide practical benefits to the developer?</li>
<li>And finally, since the responsibility of communicating with the &#8220;Model&#8221; falls to the &#8220;Controller&#8221;, are we violating MVC by using ModelBinders in the process of retrieving objects from the database?</li>
</ol>
<div>
<p>We will include a full sample project for you to download and play with yourself (written in Visual Studio 2008, compiled for ASP.NET MVC Preview 5). In order to keep the article focused on the issues and not necessarily the code, most code samples in the article will be trimmed.</p></div>
<h3>ModelBinders and RESTful URLs</h3>
<p>Because ModelBinders are not used by either the Routing Engine or ASP.NET MVC when creating links, they really play no part in the making or breaking RESTful URLs. This misconception may have arisen because the previous article showed a query string variable (&#8220;?targetCustomer=blah&#8221;) being passed to the ModelBinder. This may have made it seem that the URL was dependant on the ModelBinder, or that the ModelBinder was relying on the URL. This is not the case at all. ASP.NET MVC was passing the &#8220;targetCustomer&#8221; property to the ModelBinder only because there was a matching entry in the &#8220;RouteData&#8221; collection (which in this case happened to come from the URL).</p>
<p>The URL &#8220;/Home/Details?targetCustomer=12345&#8243; could have been expressed as &#8220;/Customers/Details/12345&#8243; just by changing how we setup our routes. So to clarify the point again: ModelBinders do not have anything to do with the forming of URLs in an MVC application. To achieve RESTful URLs in an ASP.NET application, you would use the <a rel="nofollow" href="http://blogs.msdn.com/mikeormond/archive/2008/05/14/using-asp-net-routing-independent-of-mvc.aspx">ASP.NET Routing Engine</a>.</p>
<h3>MVC ModelBinders Compared to WebForms ViewState</h3>
<p>Since web applications are built on a disconnected framework, web developers who want to simulate a stateful web application have to overcome the challenge of rebuilding the &#8216;state&#8217; of the application upon the next post to the server. In ASP.NET WebForms, the ViewState handled this responsibility. One common scenario that was handled with the ViewState is that of building a dynamic amount of input fields back into &#8220;objects&#8221; (TextBoxes, DropDownLists, etc) on the server.</p>
<p>How would ModelBinders handle that scenario? The answer to that is fairly simple, and we still achieve the desired result of transforming the HTTP post into objects on the server. Let&#8217;s assume that we have a form that allows users to enter multiple addresses to associate with their account. The form is dynamic and allows the user to keep adding, or deleting addresses in JavaScript, but ultimately we need to post this variable amount of input fields to the server and rebuild them as objects. Here is what we could do with ModelBinders:</p>
<div><span>public</span> <span>class</span> MultiAddressBinder : DefaultModelBinder<br />
{<br />
<span>public</span> <span>override</span> <span>object</span> GetValue(ControllerContext ctx,<br />
<span>string</span> modelName, Type modelType, ModelStateDictionary state)<br />
{<br />
<span>if</span> (ctx.HttpContext.Request[<span>"Line1"</span>] == <span>null</span>)<br />
{<br />
<span>return</span> <span>new</span> Address[0];<br />
}</p>
<p><span>string</span>[] line1s = ctx.HttpContext.Request.Form.GetValues(<span>&#8220;Line1&#8243;</span>);<br />
<span>string</span>[] line2s = ctx.HttpContext.Request.Form.GetValues(<span>&#8220;Line2&#8243;</span>);<br />
<span>string</span>[] cities = ctx.HttpContext.Request.Form.GetValues(<span>&#8220;City&#8221;</span>);<br />
<span>string</span>[] states = ctx.HttpContext.Request.Form.GetValues(<span>&#8220;State&#8221;</span>);<br />
<span>string</span>[] zips = ctx.HttpContext.Request.Form.GetValues(<span>&#8220;Zip&#8221;</span>);</p>
<p>List&lt;Address&gt; addresses = <span>new</span> List&lt;Address&gt;();</p>
<p><span>for</span> (<span>int</span> i = 0; i &lt; line1s.Length; i++)<br />
{<br />
addresses.Add(<span>new</span> Address(line1s[i], line2s[i], cities[i], states[i], zips[i]));<br />
}</p>
<p><span>return</span> addresses.ToArray();<br />
}<br />
}</div>
<p>Then, to register our ModelBinder so that all &#8220;Address[]&#8221; parameters for my ActionMethods will know how to build themselves, we&#8217;ll simply add this line to our Global.asax file:</p>
<div><span>protected</span> <span>void</span> Application_Start()<br />
{<br />
ModelBinders.Binders.Add(<span>typeof</span>(Address[]),<br />
<span>new</span> MultiAddressBinder());<br />
}</div>
<p>The end result will look like this:</p>
<p><img title="Dynamic address fields in an ASP.NET MVC application" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=44f56526-3db4-40db-acd7-5dfe1f4c0743" alt="Dynamic address fields in an ASP.NET MVC application" /> <img title="Dynamic address fields deserialized by ASP.NET MVC ModelBinders" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=e4ff66df-14e3-464d-a8af-b6ca9bf3c343" alt="Dynamic address fields deserialized by ASP.NET MVC ModelBinders" /></p>
<p>As is simply demonstrated, ModelBinders can be used to solve many of the same challenges that the ViewState has solved for traditional ASP.NET. Does this mean that ModelBinders and the ViewState are synonymous? By no means! The technologies are different, their design and purpose are different. The similarities are in their ability to bring a more object-oriented way of programming to stateless web applications.</p>
<h3>Restoring Stateful Objects with ModelBinders</h3>
<p>The final section of this article will deal with the controversial topic of rebuilding an object that lives in a database with a ModelBinder. It seems that rebuilding an object from anywhere else is &#8220;fine&#8221;, but the moment we introduce a stateful object, people tend to get very nervous. Are we violating cosmic laws? Let&#8217;s see.</p>
<p>In the Model-View-Controller (MVC) design pattern, the responsibility of communicating with the Model (the database) falls to the Controller. ModelBinders have nothing to do with the MVC design pattern, but rather they are simply a &#8216;helper feature&#8217; of &#8220;ASP.NET MVC&#8221; to provide a more object-oriented (and therefore more testable) approach to coding your ActionMethods.</p>
<div>
<p>At the outset of this article, we mentioned that we will judge the worthiness of this solution based on criteria mentioned above. We have just hit the first point which will be flushed out a bit more later &#8211; Improved Testability.</p></div>
<p>In the previous article we inherited from the DefaultModelBinder class, and implemented our functionality that way. In this next example we are going to add the IModelBinder attribute to our Controller itself. This way, we can still feel good about &#8220;the Controller accesses the Model&#8221; when it comes to rebuilding stateful objects, but we still get the concrete benefits that ModelBinders provide. Here&#8217;s our code (abbreviated here, full code in downloadable solution at the end):</p>
<div><span>// In the Global.asax&#8230;</span><br />
ModelBinders.Add(<span>typeof</span>(Customer),<br />
<span>new</span> StatefulObjectBinder());<span></p>
<p>// Our <span>&#8220;StatefulObjectBinder&#8221;</span> <span>class</span>&#8230;</span><br />
<span>public</span> <span>class</span> StatefulObjectBinder : DefaultModelBinder<br />
{<br />
<span>public</span> <span>override</span> <span>object</span> GetValue(ControllerContext ctx,<br />
<span>string</span> modelName, Type modelType, ModelStateDictionary state)<br />
{<br />
<span>object</span> result = <span>null</span>;</p>
<p><span>if</span> (ctx.Controller <span>is</span> IModelBinder)<br />
{<br />
result = ((IModelBinder)ctx.Controller).GetValue(ctx,<br />
modelName, modelType, state);<br />
}</p>
<p><span>return</span> result;<br />
}<br />
}</div>
<p>Now that this simple &#8220;middle-man&#8221; instance has been built, our Controller can retrieve the object from the database (as it normally would have anyway). So, you may be wondering: &#8220;why the song and dance?&#8221; (Meaning, why did we do all this work&#8230; what benefits do we get?)</p>
<p>Here are the direct benefits that we achieve from the above methodology:</p>
<ol>
<li>Since we can now code our ActionMethods to take concrete objects (instead of parsing the HttpRequest for them), we are able to write unit tests *much* easier. Take for example the simple unit test that says &#8220;if a customer was not found, redirect to the customer search page.&#8221; If you didn&#8217;t use a ModelBinder, you would have to &#8220;mock&#8221; up your database calls, and fake it so that passing in one ID will return a customer, then fake it so that passing in a different ID will return null, *then* you can finally test your logic. However, using a ModelBinder as demonstrated in this article, you can test the logic with absolutely zero mocking, just by calling &#8220;Display(new Customer())&#8221; and &#8220;Display(null)&#8221; &#8211; amazing!</li>
<li>We are holding true to the Single Responsibility Principal and the Separating Concerns patterns much stronger. If you think about it, the method &#8220;DisplayCustomer(int customerID)&#8221; has the responsibility to do two things: 1) get the customer, then 2) display it. Whereas using ModelBinders lets us make the method &#8220;DisplayCustomer(Customer c)&#8221; &#8211; now we only have one task: display it &#8211; beautiful!</li>
<li>Even developers who do not use TDD or Separating Concerns will benefit in this way: Because you can have dozens of methods that require restoring your &#8220;Customer&#8221; object (or any other object) from the database, you would have to code this logic in a single place and call it in every one of those methods. Using ModelBinders lets you skip one of those steps. You still have to code the logic, but ASP.NET MVC will automatically call it for you because you registered the type &#8220;Customer&#8221; with your ModelBinder one time &#8211; awesome!</li>
</ol>
<h3>In Conclusion</h3>
<p>The purpose of this article was to show that even though ASP.NET MVC is realtively young, it has some powerful and compelling features. Just this one topic alone of ModelBinders has contributed much buzz, including two articles here on SingingEels which doesn&#8217;t happen often for one feature.</p>
<p>In a talk that I recently watched online, Ken Robinson said that &#8220;if you&#8217;re not prepared to be wrong, you&#8217;ll never come up with anything original.&#8221; If nothing else, I hope that my risky usage of new technologies encourages you to experiment with code some more yourself.</p>
<p>Here is the full solution (Visual Studio 2008 SP1, MVC Preview 5). Enjoy: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=647429db-6fec-4180-8265-94e4701005fe">SingingEels_MVC_ModelBindersPart2.zip</a></p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;bodytext=The%20previous%20article%20on%20ModelBinders%20gave%20a%20basic%20introduction%2C%20showed%20a%20few%20code%20samples%2C%20and%20showed%20a%20creative%20usage%20of%20ModelBinders.%20However%2C%20some%20questions%20arose%2C%20some%20claims%20were%20challenged%20%28particularly%20about%20comparing%20ModelBinders%20to%20the%20WebFo" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;notes=The%20previous%20article%20on%20ModelBinders%20gave%20a%20basic%20introduction%2C%20showed%20a%20few%20code%20samples%2C%20and%20showed%20a%20creative%20usage%20of%20ModelBinders.%20However%2C%20some%20questions%20arose%2C%20some%20claims%20were%20challenged%20%28particularly%20about%20comparing%20ModelBinders%20to%20the%20WebFo" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;annotation=The%20previous%20article%20on%20ModelBinders%20gave%20a%20basic%20introduction%2C%20showed%20a%20few%20code%20samples%2C%20and%20showed%20a%20creative%20usage%20of%20ModelBinders.%20However%2C%20some%20questions%20arose%2C%20some%20claims%20were%20challenged%20%28particularly%20about%20comparing%20ModelBinders%20to%20the%20WebFo" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;Title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;headline=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=The%20previous%20article%20on%20ModelBinders%20gave%20a%20basic%20introduction%2C%20showed%20a%20few%20code%20samples%2C%20and%20showed%20a%20creative%20usage%20of%20ModelBinders.%20However%2C%20some%20questions%20arose%2C%20some%20claims%20were%20challenged%20%28particularly%20about%20comparing%20ModelBinders%20to%20the%20WebFo" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;bm_description=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;t=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;h=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;story_title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;title=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmodel-binders-in-asp-net-mvc-part-2%2F&amp;submitHeadline=Model%20Binders%20in%20ASP.NET%20MVC%20-%20Part%202&amp;submitSummary=The%20previous%20article%20on%20ModelBinders%20gave%20a%20basic%20introduction%2C%20showed%20a%20few%20code%20samples%2C%20and%20showed%20a%20creative%20usage%20of%20ModelBinders.%20However%2C%20some%20questions%20arose%2C%20some%20claims%20were%20challenged%20%28particularly%20about%20comparing%20ModelBinders%20to%20the%20WebFo&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/model-binders-in-asp-net-mvc-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC AJAX Sites That Gracefully Degrade</title>
		<link>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade/</link>
		<comments>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 14:50:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=12</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade/"><img align="left" hspace="5" width="100" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=23d3731b-07bd-46d6-b74e-c215b65f696c" class="alignleft wp-post-image tfe" alt="ASP.NET MVC - AJAX application that gracefully degrades part 1" title="ASP.NET MVC - AJAX application that gracefully degrades part 1" /></a>As AJAX-enabled web sites continue to grow in popularity, the development community continues to try to solve the common problems of using AJAX. One big concern that used to require a lot of work is that of gracefully degrading your AJAX site for non-capable browsers. Thankfully, the ASP.NET MVC framework makes this an easy task. [...]]]></description>
			<content:encoded><![CDATA[<p>As AJAX-enabled web sites continue to grow in popularity, the development community continues to try to solve the common problems of using AJAX. One big concern that used to require a lot of work is that of gracefully degrading your AJAX site for non-capable browsers. Thankfully, the ASP.NET MVC framework makes this an easy task.</p>
<p>This article will demonstrate some basic AJAX functionality that comes with the ASP.NET MVC framework. We&#8217;ll also see how simple it is to get the same usefulness of our web application even if we disable JavaScript capabilities in our browser.</p>
<p>Our demo site will have a &#8220;news&#8221; section where users can view the latest news, and subscribe to our newsletter. This functionality will be provided through AJAX calls. Our goal is to achieve this same functionality for non-ajax-enabled browsers without duplicating a lot of work.</p>
<h3>AJAX Extensions in ASP.NET MVC</h3>
<p>If you&#8217;ve been playing around in ASP.NET MVC even a little, you likely have noticed how simple it is to use AJAX in your web app. Take for example creating a link to our &#8220;News List&#8221; page (which is actually in our demo app). To make a standard link that goes to the &#8220;News&#8221; controller and executes the &#8220;List&#8221; action would look something like this:</p>
<div><span>&lt;%</span><span>=</span> Html.ActionLink(<span>&#8220;View News&#8221;</span>, <span>&#8220;List&#8221;</span>, <span>&#8220;News&#8221;</span>) <span>%&gt;</span></div>
<p>But what would we do if we wanted that link to use AJAX to pull the latest news and update a simple &lt;div&gt; on our page? All we would have to do is use the &#8220;Ajax.ActionLink&#8221; method, and supply an extra parameter that has the &#8216;AJAX options&#8217; in there. Here&#8217;s what that would look like:</p>
<div><span>&lt;%</span><span>=</span><br />
Ajax.ActionLink(<span>&#8220;View News&#8221;</span>, <span>&#8220;List&#8221;</span>, <span>&#8220;News&#8221;</span>,<br />
<span>new</span> AjaxOptions<br />
{<br />
HttpMethod = <span>&#8220;POST&#8221;</span>,<br />
UpdateTargetId = <span>&#8220;news-list&#8221;</span>,<br />
InsertionMode = InsertionMode.Replace,<br />
})<br />
<span>%&gt;</span></div>
<p>That code will build an HTML link that also has some JavaScript in the onclick event for handling the AJAX stuff. It&#8217;s important to know that since this is a regular old HTML link, if the JavaScript part fails (either due to a bug, or if scripts are disabled), then the browser will navigate to the URL as if there was no JavaScript at all. And that brings us to the next point!</p>
<h3>Coding Your ActionMethod to Detect AJAX Requests</h3>
<p>Because one of our main goals is not to have to duplicate a lot of work, we want the same code to run for the user if he&#8217;s using AJAX or not. The only part that has to change really is the &#8220;View&#8221; that gets sent back to the client. In order to accomplish this, we&#8217;re going to use the &#8220;IsMvcAjaxRequest&#8221; method that comes in the System.Web.Mvc.Ajax namespace (in the AjaxExtensions class).</p>
<div><span>Update: Oct 22 2008</span>Originally, I didn&#8217;t know about the &#8220;IsMvcAjaxRequest&#8221; method, so I had a code snippet here that showed how to make our own method by looking for the &#8220;__MVCASYNCPOST&#8221; form field. Due to a comment below, I&#8217;ve removed the code snippet that had the extension method here.</p>
<p>As was mentioned by <a rel="nofollow" href="http://www.lostintangent.com/">Jonathan Carter</a> (who&#8217;s a Technical Evangelist for Microsoft), the MVC Beta already includes the extension method above. Now all we have to do is call the &#8220;IsMvcAjaxRequest&#8221; method. Example:</p>
<div><span>if</span> (Request.IsMvcAjaxRequest())<br />
{<span><br />
// Do async things here&#8230;</span><br />
}</div>
</div>
<p>And then to use it in our ActionMethod, here&#8217;s what we would do:</p>
<div><span>public</span> ActionResult List()<br />
{<br />
<span>this</span>.ViewData.Model = <span>this</span>.GetNewsItems();</p>
<p><span>if</span> (<span>this</span>.Request.IsMvcAjaxRequest() == <span>false</span>)<br />
{<span><br />
// Non AJAX requests see the entire ViewPage.</span><br />
<span>return</span> View();<br />
}<br />
<span>else</span><br />
{<span><br />
// AJAX requests just <span>get</span> a trimmed down UserControl.</span><br />
<span>return</span> View(<span>&#8220;List_AJAX&#8221;</span>);<br />
}<br />
}</div>
<p>Notice that we don&#8217;t have to duplicate any logic for getting the latest news. The only bit of custom logic that is needed is to determine which view to send. This can be done for form posts too (as you can see in the screen shots below or by downloading the project at the end of the article). Here&#8217;s what the results of the above will look like for an AJAX enabled, and a non-AJAX enabled browser:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 1" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=23d3731b-07bd-46d6-b74e-c215b65f696c" alt="ASP.NET MVC - AJAX application that gracefully degrades part 1" /></p>
<p>After clicking on the &#8220;view news&#8221; link with AJAX enabled:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 2" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=1f6199f8-129e-485c-a08a-8ed830114b55" alt="ASP.NET MVC - AJAX application that gracefully degrades part 2" /></p>
<p>After clicking &#8220;view news&#8221; with JavaScript (and therefore AJAX) disabled:</p>
<p><img title="ASP.NET MVC - AJAX application that gracefully degrades part 3" src="http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=cd8aa437-45c4-4f66-b84e-e5cd44256583" alt="ASP.NET MVC - AJAX application that gracefully degrades part 3" /></p>
<p>If you notice the URLs in the second and third screen shots, you&#8217;ll see that with AJAX disabled the browser simply navigated to where the link was pointing. Thus, a graceful degradation was achieved.</p>
<h3>Conclusion</h3>
<p>With little effort, we achieved an AJAX enabled ASP.NET MVC site, and cleanly/gracefully allowed non-AJAX users to enjoy the same benefits of our demo app. The project was written in Visual Studio 2008 SP1 with the ASP.NET MVC Beta 1 bits installed. Because the DLL&#8217;s are bin-deployed, you should be able to download and run the project even if you don&#8217;t have the MVC beta install. Check it out for yourself: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=997f0db8-ac1d-4405-a336-eb0b8416d12e">SingingEels_MvcAjaxMashup.zip</a></p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;bodytext=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;notes=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;t=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;annotation=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;Title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;headline=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;bm_description=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;t=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;h=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;story_title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;title=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fmvc-ajax-sites-that-gracefully-degrade%2F&amp;submitHeadline=MVC%20AJAX%20Sites%20That%20Gracefully%20Degrade&amp;submitSummary=As%20AJAX-enabled%20web%20sites%20continue%20to%20grow%20in%20popularity%2C%20the%20development%20community%20continues%20to%20try%20to%20solve%20the%20common%20problems%20of%20using%20AJAX.%20One%20big%20concern%20that%20used%20to%20require%20a%20lot%20of%20work%20is%20that%20of%20gracefully%20degrading%20your%20AJAX%20site%20for%20non&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/mvc-ajax-sites-that-gracefully-degrade/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Custom View Engine in ASP.NET MVC</title>
		<link>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc/</link>
		<comments>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 14:43:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=9</guid>
		<description><![CDATA[<a href="http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>ASP.NET is an awesome web development platform that many developers wouldn&#8217;t ever want to part with. However, if given the option, there are definitely parts of the package that you may want to swap out. This article will show you how easy it can be to break away from the &#8220;inline-ASP&#8221; or &#8220;Web Forms&#8221; world, [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET is an awesome web development platform that many developers wouldn&#8217;t ever want to part with. However, if given the option, there are definitely parts of the package that you may want to swap out. This article will show you how easy it can be to break away from the &#8220;inline-ASP&#8221; or &#8220;Web Forms&#8221; world, and delve into your own custom ViewEngine.</p>
<p>It&#8217;s true that much of the ASP.NET framework has been pluggable for a while (example: Membership and Profiles). Also, we have always had a choice of server-side language from the start (example: C# or VB). But only recently with the birth of ASP.NET MVC do we have a clean and straight forward solution to swap out the &#8220;ASP&#8221; language for our own.</p>
<p>By the end of this article, we will have made our own language (called &#8220;HoTMeaT&#8221;), we will have registered our custom ViewEngine and we will notice just how easy it is to swap out this seemingly &#8216;fixed&#8217; area of ASP.NET. At the end of the article, you can download the entire demo project &#8211; including the source code for the language.</p>
<div>
<p>It&#8217;s important to note that we have always had the option of writing a custom HttpHandler. But that meant that we have to take over all aspects of the request life-cycle. But with ASP.NET MVC&#8217;s custom &#8220;ViewEngine&#8221; capabilities, we are *ONLY* tapping into the slice of the cycle that takes a file and outputs HTML. The &#8220;controller&#8221; will still function the same; security will still be enforced, etc.</p></div>
<h3>A Sneak Peek Into the Language</h3>
<p>Before we get into a &#8220;step-by-step&#8221; of how to write our own ViewEngine, I want to show you how amazing this new &#8220;HoTMeaT&#8221; language is going to look. Since I&#8217;m currently on a WPF kick, I&#8217;m going to copy some of that great functionality by taking &#8220;binding&#8221; to the next level.</p>
<p>Here&#8217;s the code for the Customers/List page. We won&#8217;t need to do anything different in our code, because really, the &#8220;Controller&#8221; doesn&#8217;t care about how &#8220;View&#8221; will look. It&#8217;s only responsible for getting the data.</p>
<div><span>public</span> ActionResult List()<br />
{<br />
ViewData[<span>"People"</span>] = <span>new</span> Person[]<br />
{<br />
<span>new</span> Person{FirstName=<span>&#8220;Timothy&#8221;</span>, LastName=<span>&#8220;Khouri&#8221;</span>},<br />
<span>new</span> Person{FirstName=<span>&#8220;Jonathan&#8221;</span>, LastName=<span>&#8220;Carter&#8221;</span>},<br />
<span>new</span> Person{FirstName=<span>&#8220;Travis&#8221;</span>, LastName=<span>&#8220;Sheppard&#8221;</span>},<br />
};</p>
<p><span>return</span> View();<br />
}</div>
<p>Now, how would you display a list of those people in your own custom HTML template? Well, if you were using WebForms (traditional ASP.NET), you would probably use a Repeater and then do data binding in the code behind. If you are using ASP.NET MVC, you would probably just write a for-loop with sloppy inline ASP. But, with HoTMeaT&#8230;</p>
<div>&lt;<span>ul</span>&gt;<br />
&lt;<span>listView</span> <span>source</span><span>=&#8221;{ViewData People}&#8221;</span>&gt;<br />
&lt;<span>itemTemplate</span>&gt;<br />
&lt;<span>li</span>&gt;{Binding LastName}, {Binding FirstName}&lt;<span>/li</span>&gt;<br />
&lt;<span>/itemTemplate</span>&gt;<br />
&lt;<span>/listView</span>&gt;<br />
&lt;<span>/ul</span>&gt;</div>
<p>And here&#8217;s the HTML that it generates:</p>
<div>&lt;<span>ul</span>&gt;<br />
&lt;<span>li</span>&gt;Khouri, Timothy&lt;<span>/li</span>&gt;<br />
&lt;<span>li</span>&gt;Carter, Jonathan&lt;<span>/li</span>&gt;<br />
&lt;<span>li</span>&gt;Sheppard, Travis&lt;<span>/li</span>&gt;<br />
&lt;<span>/ul</span>&gt;</div>
<p>Now that&#8217;s pretty cool. But before we go on, there is a myth that needs to be dispelled. I&#8217;ve heard multiple people say that if you use / build a custom ViewEngine that you&#8217;ll suffer some great performance hit. That is completely not true. The HoTMeaT view engine (which parses the HTML for custom tags, and performs data binding at runtime for each request) runs at 0.00015 seconds. That means that it can handle hundreds of requests per second from my laptop. So no, there are no performance concerns (unless of course you&#8217;re a bad programmer).</p>
<h3>Creating and Registering your Custom ViewEngine</h3>
<p>ASP.NET MVC was built to allow developers to change certain parts of the pipe-line in a similar way. For example, creating and registering a ViewEngine is much like creating your own <a rel="nofollow" href="http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx">custom model binder</a> and then registering it at runtime. We&#8217;ll create a class that inherits from &#8220;IViewEngine&#8221;. Then (just like with registering a ModelBinder) we&#8217;ll register our custom ViewEngine in the Global.asax file. Here&#8217;s the code:</p>
<div><span>protected</span> <span>void</span> Application_Start()<br />
{<span><br />
//&#8230; other things up here.</span><span></p>
<p>// I want to REMOVE the ASP.NET ViewEngine&#8230;</span><br />
ViewEngines.Engines.Clear();<span></p>
<p>// and then <span>add</span> my own <img src='http://www.comteken.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span><br />
ViewEngines.Engines.Add(<span>new</span> HoTMeaTViewEngine());<br />
}</div>
<p>Inside of our ViewEngine class we only have to provide the functionality for two methods &#8211; CreateView and CreatePartialView. Those methods will return an instance of a class that implements &#8220;IView&#8221;. The View class has one method in it &#8211; Render.</p>
<div>
<p>While it&#8217;s true that your custom ViewEngine only needs to implement the IViewEngine interface, I went a small step further and implemented the &#8220;VirtualPathProviderViewEngine&#8221; class. That class implements IViewEngine, but it also provides a little extra functionality that helps us tell MVC where to look for our View files.</p></div>
<p>Here&#8217;s the code for our ViewEngine:</p>
<div><span>public</span> <span>class</span> HoTMeaTViewEngine : VirtualPathProviderViewEngine<br />
{<br />
<span>public</span> HoTMeaTViewEngine()<br />
{<span><br />
// This <span>is</span> <span>where</span> we tell MVC <span>where</span> to look <span>for</span> our files. This says</span><span><br />
// to look <span>for</span> a file at <span>&#8220;Views/Controller/Action.html&#8221;</span></span><br />
<span>base</span>.ViewLocationFormats = <span>new</span> <span>string</span>[] { <span>&#8220;~/Views/{1}/{0}.html&#8221;</span> };</p>
<p><span>base</span>.PartialViewLocationFormats = <span>base</span>.ViewLocationFormats;<br />
}</p>
<p><span>protected</span> <span>override</span> IView CreateView(ControllerContext context, <span>string</span> viewPath, <span>string</span> masterPath)<br />
{<br />
<span>return</span> <span>new</span> HoTMeaTView(viewPath, masterPath);<br />
}</p>
<p><span>protected</span> <span>override</span> IView CreatePartialView(ControllerContext context, <span>string</span> partialPath)<br />
{<br />
<span>return</span> <span>new</span> HoTMeaTView(partialPath, <span>&#8220;&#8221;</span>);<br />
}<br />
}</div>
<h3>How Do I Build my View?</h3>
<p>So now that we&#8217;ve created our ViewEngine and registered it in the Global.asax, we have to actually do something in our View class. As was mentioned above, the only method that we have to implement is &#8220;Render&#8221;, which passes in the viewContext (which gives us the Controller, ViewData, RouteData, etc) and a TextWriter for us to render to. So, if you wanted to make a *very* basic view engine that simply shells out a static file, you could do this:</p>
<div><span>public</span> <span>class</span> MyView : IView<br />
{<br />
<span>public</span> HoTMeaTView(<span>string</span> viewPath)<br />
{<br />
<span>this</span>.ViewPath = viewPath;<br />
}</p>
<p><span>public</span> <span>string</span> ViewPath { <span>get</span>; <span>private</span> <span>set</span>; }</p>
<p><span>public</span> <span>void</span> Render(ViewContext viewContext, TextWriter writer)<br />
{<br />
<span>string</span> filePath = viewContext.HttpContext.Server.MapPath(<span>this</span>.ViewPath);</p>
<p><span>string</span> fileContents = File.ReadAllText(filePath);</p>
<p>writer.Write(File.ReadAllText(fileContents);<br />
}<br />
}</p></div>
<p>And that&#8217;s really it! We&#8217;ve just made our own ViewEngine which creates a simple View that writes out the contents of a file. Now, you may be wondering what happened to my great new &#8220;HoTMeaT&#8221; language where we could do all that great data-binding right in our HTML. If you want to see all that code, then you can download the source project and take a look at the code yourself. That code is intentionally left that so as not to detract from the main point of the article. So here&#8217;s the full demo project with all of the code including the &#8220;HotMeatViewEngine&#8221;: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=d8db8937-47a3-49f7-9036-906ee71d07a4">SingingEels_CustomMvcViewEngine.zip</a></p>
<h3>So What Is &#8220;HoT MeaT&#8221;?</h3>
<p>Back in college I had a very &#8220;build it yourself to learn it&#8221; mentality (and really I still do today). So, I wanted to understand Sockets and client/server programming, so I made my own &#8220;web server&#8221;. All it did was shell out static HTML files. Then for some reason, my friend and I decided that we would make our own language: HTMT (&#8220;Hyper Text Meta Transformation&#8221; &#8211; aka &#8220;HoT MeaT&#8221;). I think we were bored in C++ 101, and bordom leads to invention! We got to the point of displaying the time on the server and basically stopped there. So that&#8217;s the story behind the name.</p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;bodytext=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;notes=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;t=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;annotation=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;Title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;headline=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;bm_description=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;t=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;h=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;story_title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;title=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC%20-%20http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fweb-programming%2Fcreating-a-custom-view-engine-in-asp-net-mvc%2F&amp;submitHeadline=Creating%20a%20Custom%20View%20Engine%20in%20ASP.NET%20MVC&amp;submitSummary=ASP.NET%20is%20an%20awesome%20web%20development%20platform%20that%20many%20developers%20wouldn%27t%20ever%20want%20to%20part%20with.%20However%2C%20if%20given%20the%20option%2C%20there%20are%20definitely%20parts%20of%20the%20package%20that%20you%20may%20want%20to%20swap%20out.%20This%20article%20will%20show%20you%20how%20easy%20it%20can%20be%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/web-programming/creating-a-custom-view-engine-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

