<?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>Zenji Web Development &#187; ASP.NET MVC</title>
	<atom:link href="http://www.zenjiwebworks.com/category/software-development/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zenjiwebworks.com</link>
	<description>Website development and consultancy</description>
	<lastBuildDate>Fri, 25 Sep 2009 08:10:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC ActionFilters and Response.Redirect</title>
		<link>http://www.zenjiwebworks.com/2008/12/19/aspnet-mvc-actionfilters-and-responseredirect/</link>
		<comments>http://www.zenjiwebworks.com/2008/12/19/aspnet-mvc-actionfilters-and-responseredirect/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 16:28:53 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Gotchas]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.zenjiwebworks.com/2008/12/19/aspnet-mvc-actionfilters-and-responseredirect/</guid>
		<description><![CDATA[There is an issue with using the following code in an action filter:



filterContext.HttpContext.Response.Redirect&#40;loginUrl, true&#41;;



which is a bit of code quite common in an authentication filter. The problem is it will throw a ThreadAbortException as soon as you put your code on a live site resulting in the error page for anyone who is not logged [...]]]></description>
			<content:encoded><![CDATA[<p>There is an issue with using the following code in an action filter:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">filterContext.<span class="me1">HttpContext</span>.<span class="me1">Response</span>.<span class="me1">Redirect</span><span class="br0">&#40;</span>loginUrl, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>which is a bit of code quite common in an authentication filter. The problem is it will throw a ThreadAbortException as soon as you put your code on a live site resulting in the error page for anyone who is not logged in. This is probably not what you want since you are attempting to redirect them to the login page.</p>
<p>Now it is a little unfair to say that it&#8217;s an issue because that is the intended functionality. But it doesn&#8217;t fit well with MVC controllers, especially since Microsoft&#8217;s official suggestion is to set the endResponse flag to false. That is just downright misleading since your controller action (which the filter just decided you can&#8217;t access) is still executed after the filter fails authentication &#8211; resulting in the action code being executed anyway whilst you see the login screen on the browser and assume it all went swimmingly! Meanwhile those files that this user shouldn&#8217;t have access to got deleted.</p>
<p>Yeah, that&#8217;s what I just did during testing whilst proving to myself that you can&#8217;t fake the URL as a non-registered user. Imagine my surprise. It&#8217;s true that the chances of a malicious user actually working out the URL might be low, but that&#8217;s no reason to leave the door open.</p>
<p>Oh, and your unit tests probably won&#8217;t catch this because the filters don&#8217;t get executed by the unit tests. But I digress&#8230;</p>
<p>To cut a long story short, this bit of code in your base controller (assuming you have one, otherwise stick in in all of the controllers) will prevent the issue:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected override <span class="kw4">void</span> OnException<span class="br0">&#40;</span>ExceptionContext filterContext<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>filterContext.<span class="me1">Exception</span> is ThreadAbortException<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; filterContext.<span class="me1">ExceptionHandled</span> = <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.zenjiwebworks.com/2008/12/19/aspnet-mvc-actionfilters-and-responseredirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Html.DropDownList in ASP.NET MVC</title>
		<link>http://www.zenjiwebworks.com/2008/12/09/using-htmldropdownlist-in-aspnet-mvc/</link>
		<comments>http://www.zenjiwebworks.com/2008/12/09/using-htmldropdownlist-in-aspnet-mvc/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 14:07:11 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.zenjiwebworks.com/2008/12/09/using-htmldropdownlist-in-aspnet-mvc/</guid>
		<description><![CDATA[This is something I think is particularly poorly documented for the MVC framework. It took me a long time Googling and, in the end, guessing in order to get it to work correctly (and it&#8217;s still not how I&#8217;d like it, but there you go). Simply put, this is the code in the Controller:



var portfolioSelect [...]]]></description>
			<content:encoded><![CDATA[<p>This is something I think is particularly poorly documented for the MVC framework. It took me a long time Googling and, in the end, guessing in order to get it to work correctly (and it&#8217;s still not how I&#8217;d like it, but there you go). Simply put, this is the code in the Controller:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">var portfolioSelect = new Dictionary&lt;string, object&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">foreach <span class="br0">&#40;</span>var portfolio in portfolioList<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; portfolioSelect.<span class="me1">Add</span><span class="br0">&#40;</span>portfolio.<span class="me1">ID</span>, portfolio.<span class="me1">Name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">ViewData<span class="br0">&#91;</span><span class="st0">&quot;PortfolioSelectDropDownList&quot;</span><span class="br0">&#93;</span> = new SelectList<span class="br0">&#40;</span>portfolioSelect, <span class="st0">&quot;key&quot;</span>, <span class="st0">&quot;value&quot;</span>, id<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And this is the code in the View:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;%=Html.<span class="me1">DropDownList</span><span class="br0">&#40;</span><span class="st0">&quot;PortfolioSelectDropDownList&quot;</span><span class="br0">&#41;</span>%&gt;</div>
</li>
</ol>
</div>
<p>I know it&#8217;s still only in Beta, but I think it&#8217;s time some of the Microsoft boys tidied up their otherwise useful blogs to remove and update many incorrect usage instructions for the latest cut of the framework. After all, it&#8217;s the only documentation we have at the moment.</p>
<p>One more issue I&#8217;ve found is the name that is assigned to the resulting &lt;select&gt; tag. It&#8217;s a bit irritating as I may not want the name to be the same is the key that is used in the ViewData (which is what it uses here, i.e. &#8220;PortfolioSelectDropDownList&#8221; in my case). That messes up my typed view. The only way I&#8217;ve found around this is to use the name of the variable in my typed view for the ViewData key, e.g. &#8220;Project.PortfolioID&#8221;. The result is that I get a select list and when the form is posted back my Project object is populated with the selected value.</p>
<p>To be honest it has a certain elegance to it so it may even be the intended usage! Still seems a bit strange to me though&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zenjiwebworks.com/2008/12/09/using-htmldropdownlist-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Strongly-Typed Views in ASP.NET MVC Beta</title>
		<link>http://www.zenjiwebworks.com/2008/12/05/creating-strongly-typed-views-in-aspnet-mvc-beta/</link>
		<comments>http://www.zenjiwebworks.com/2008/12/05/creating-strongly-typed-views-in-aspnet-mvc-beta/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 11:42:01 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.zenjiwebworks.com/2008/12/05/creating-strongly-typed-views-in-aspnet-mvc-beta/</guid>
		<description><![CDATA[One of the big problems with the early adoption of any framework is the documentation. No books are released yet, if they are ever going to be, and so turning to Google is your best bet. In fact it&#8217;s your only option and my own preference is using Google over books anyway.
But frameworks change during [...]]]></description>
			<content:encoded><![CDATA[<p>One of the big problems with the early adoption of any framework is the documentation. No books are released yet, if they are ever going to be, and so turning to Google is your best bet. In fact it&#8217;s your only option and my own preference is using Google over books anyway.</p>
<p>But frameworks change during development and you come across the issue that all of the top hits for your query are based on previous versions. When that previous version did it differently you&#8217;re stuck! I had this problem today when trying to create strongly-typed views. I love strongly-typed anythings, but I&#8217;m just a sucker for having a project that is more likely to work when another developer makes an edit and doesn&#8217;t bother fully checking the effects of their changes. It&#8217;s another safety net, and I&#8217;m a sucker for safety nets as well.</p>
<p>So, this is how I did it in bite-sized chunks. First the controller function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">public ActionResult Index<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var ContentObject = new ContentClass<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> View<span class="br0">&#40;</span>ContentModel.<span class="me1">Load</span><span class="br0">&#40;</span>ContentObject<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Next I created a new view by right-clicking the above method (bear in mind that I&#8217;m using Visual Studio 2008) and checking the &#8216;Create a strongly-typed view&#8217; before entering the full name of the object type (ContentClass). That produces an aspx.cs which looks something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">public partial class Index : ViewPage&lt;ContentClass&gt;</div>
</li>
</ol>
</div>
<p>So far so good, but where I really came unstuck was actually using this in the view. Turns out it&#8217;s simpler than ever. All you need to use is the ViewData.Model parameter, which is automatically typed to whatever you set the view up as previously:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;%</div>
</li>
<li class="li1">
<div class="de1">foreach <span class="br0">&#40;</span>var portfolio in ViewData.<span class="me1">Model</span>.<span class="me1">Portfolios</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">%&gt;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &lt;p&gt;&lt;%= portfolio.<span class="me1">ID</span> %&gt;&lt;/p&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;%</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">%&gt;</div>
</li>
</ol>
</div>
<p>All that I have to do now is pass that through to a UserControl. I&#8217;m assuming I can strongly-type those as well. We shall see.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zenjiwebworks.com/2008/12/05/creating-strongly-typed-views-in-aspnet-mvc-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying ASP.NET MVC Applications</title>
		<link>http://www.zenjiwebworks.com/2008/11/28/deploying-aspnet-mvc-applications/</link>
		<comments>http://www.zenjiwebworks.com/2008/11/28/deploying-aspnet-mvc-applications/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 21:56:31 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.zenjiwebworks.com/2008/11/28/deploying-aspnet-mvc-applications/</guid>
		<description><![CDATA[Now I&#8217;m a huge fan of the ASP.NET MVC framework. So much so that I have taken to porting across applications already started using the Castle Monorail framework. Why? Well, there&#8217;s just a nice, warm, supported feeling when using a framework that is built-in to your chosen development platform.
Or at least that&#8217;s what you&#8217;d think. You [...]]]></description>
			<content:encoded><![CDATA[<p>Now I&#8217;m a huge fan of the ASP.NET MVC framework. So much so that I have taken to porting across applications already started using the Castle Monorail framework. Why? Well, there&#8217;s just a nice, warm, supported feeling when using a framework that is built-in to your chosen development platform.</p>
<p>Or at least that&#8217;s what you&#8217;d think. You know how it is &#8211; you get it all working on your lovely development machine and then once the decision has been made on where to host the final result, you upload your application and&#8230;bang&#8230;</p>
<pre>HttpException (0x80004005): The incoming request does not match any route.</pre>
<p>Oh yes, that works so well.</p>
<p>Suddenly that belief that, finally, someone has produced a framework which is so nicely integrated with your target platform comes crashing down around your head as you weep softly into your keyboard. No, ASP.NET MVC is not actually supported on anything under IIS7. Does your host offer IIS7 at the moment? I guess not.</p>
<p>Now don&#8217;t get me wrong, it does <em>work</em> on IIS6 but you have to mangle it a bit and unless you pay a fortune each month for access to your own virtual server then you&#8217;re likely to lose those pretty urls. That&#8217;s not the same as being <em>supported</em>. For those that have spent the last two hours Googling and still getting the same problem, here is the man with the answer:</p>
<p><a href="http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx">http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zenjiwebworks.com/2008/11/28/deploying-aspnet-mvc-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
