<?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>ByteCyclist.com &#187; ASP.Net</title>
	<atom:link href="http://www.bytecyclist.com/tag/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bytecyclist.com</link>
	<description>A site made of bytes by a cyclist.</description>
	<lastBuildDate>Tue, 20 Sep 2011 20:49:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Json.Net Action Result with Asp.Net MVC</title>
		<link>http://www.bytecyclist.com/2010/10/08/json-dot-net-action-result-with-asp-dot-net-mvc/</link>
		<comments>http://www.bytecyclist.com/2010/10/08/json-dot-net-action-result-with-asp-dot-net-mvc/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 14:00:01 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Json]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=284</guid>
		<description><![CDATA[Well this has probably been done 100 times before but so what, the world could always use more code. When working with ASP.Net MVC I started and fell in love with James Newton-King&#8217;s Json.Net library. It is simple awesome and &#8230; <a href="http://www.bytecyclist.com/2010/10/08/json-dot-net-action-result-with-asp-dot-net-mvc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Well this has probably been done 100 times before but so what, the world could always use more code. When working with ASP.Net MVC I started and fell in love with <a href="http://james.newtonking.com/pages/json-net.aspx">James Newton-King&#8217;s Json.Net library</a>.  It is simple awesome and does an amazing job in different parts of my app.  The control over the serialzation and deserialization is very good and thought it would be well suited for my MVC application.</p>
<p>You might be thinking, why not just use the one that comes with .Net. It&#8217;s in the box and is just as good. Well my preference was control, I wanted to control my model better and I wanted to output Json differently in different situations. So I created a new ActionResult that does just that but with the <a href="http://james.newtonking.com/pages/json-net.aspx">Json.Net library</a>.  Enjoy!</p>
<p>Example usage in any controller action:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> ActionResult List<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> Year, <span style="color: #6666cc; font-weight: bold;">int</span> Month<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Year <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        Year <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Year</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Month <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        Month <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Month</span><span style="color: #008000;">;</span>
&nbsp;
    var calendarMonth <span style="color: #008000;">=</span> _calendarService<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCalendarMonth</span><span style="color: #008000;">&#40;</span>Year, Month, <span style="color: #008000;">!</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsAdmin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> JsonNetResult<span style="color: #008000;">&#40;</span>calendarMonth<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The code is below, but it can also be downloaded <a href="http://gist.github.com/gists/606739/download">here</a> and found on <a href="http://github.com">Github</a> <a href="http://gist.github.com/606739">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Mvc</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Newtonsoft.Json</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Newtonsoft.Json.Converters</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> com<span style="color: #008000;">.</span><span style="color: #0000FF;">bytecyclist</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> JsonNetResult <span style="color: #008000;">:</span> ActionResult
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Encoding ContentEncoding <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ContentType <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Data <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> JsonSerializerSettings SerializerSettings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Formatting Formatting <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> JsonNetResult<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            SerializerSettings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JsonSerializerSettings<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            SerializerSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">Converters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> JavaScriptDateTimeConverter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Formatting <span style="color: #008000;">=</span> Formatting<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> JsonNetResult<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> data<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Data <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> ExecuteResult<span style="color: #008000;">&#40;</span>ControllerContext context<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>context <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;context&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var response <span style="color: #008000;">=</span> context<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">;</span>
            response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>ContentType<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> ContentType <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;application/json&quot;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ContentEncoding <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentEncoding</span> <span style="color: #008000;">=</span> ContentEncoding<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Data <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
            var writer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JsonTextWriter<span style="color: #008000;">&#40;</span>response<span style="color: #008000;">.</span><span style="color: #0000FF;">Output</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> Formatting <span style="color: #008000;">=</span> Formatting <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
            var serializer <span style="color: #008000;">=</span> JsonSerializer<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>SerializerSettings<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            serializer<span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span>writer, Data<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            writer<span style="color: #008000;">.</span><span style="color: #0000FF;">Flush</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2010/10/08/json-dot-net-action-result-with-asp-dot-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Asp.Net MVC Navigation Menu</title>
		<link>http://www.bytecyclist.com/2010/10/01/simple-asp-net-mvc-navigation-menu/</link>
		<comments>http://www.bytecyclist.com/2010/10/01/simple-asp-net-mvc-navigation-menu/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 19:44:59 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[MVC 2]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=304</guid>
		<description><![CDATA[Well it&#8217;s October first, it has been a really long time since I have last written anything. Oh well. Recently I have been working on applying some updates to an MVC site I maintain, as usual I updated the dependencies, &#8230; <a href="http://www.bytecyclist.com/2010/10/01/simple-asp-net-mvc-navigation-menu/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Well it&#8217;s October first, it has been a really long time since I have last written anything. Oh well.  Recently I have been working on applying some updates to an MVC site I maintain, as usual I updated the dependencies, ran some tests, and looked over the code.  It&#8217;s been a while since I last opened the project since it has been in production.  Of course, I was not happy with a couple things in the application and was constantly fixing the same bugs in the navigation menu.</p>
<p>There are always the same questions with a navigation menu, how do you render the html, how do you know what item is selected, how to achieve 2 level menus, and how do you trim by security.  To give some history; I went from defining only a single level with no selection (action links in an un-ordered list).  Then I found a project on <a href="http://codeplex.com/">codeplex</a> called <a href="http://mvcsitemap.codeplex.com/">ASP.NET MVC SiteMap Provider</a>, this is great it has a ton of features, fits the provider model of ASP.Net, easy to define and use in simple cases.  Unfortunately, it is not as mature as I would like.  Plus it adds another dependency that is hard to work with and does not always work the way I would expect it to.  Needless to say it is buggy (my opinion and experience only, others may differ).</p>
<p>Which brings me to my subject line.  I wanted to create my own navigation system and have full control over the levels, rendering and selection of the items. Perfect, I needed a partial to render the menu and top level items in my own un-ordered list. I also needed some helpers to render those items in my partial, in this case the top level is a list of my controllers.</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">&lt;ul id=&quot;navigation&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Home&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Calendar&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Media&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Messages&quot;</span>, <span style="color: #cc0000;">&quot;Forum&quot;</span>, <span style="color: #cc0000;">&quot;Index&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Goods&quot;</span>, <span style="color: #cc0000;">&quot;Catalog&quot;</span>, <span style="color: #cc0000;">&quot;Index&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Links&quot;</span>, <span style="color: #cc0000;">&quot;Link&quot;</span>, <span style="color: #cc0000;">&quot;Index&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Contact&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">LoginStatusNavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&lt;/ul&gt;
&nbsp;
&lt;div class=&quot;clearboth&quot;&gt;&lt;/div&gt;
&nbsp;
&lt;ul id=&quot;subnavigation&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">SubNavListItems</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">NavItem</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Join Our Mailing List!&quot;</span>, <span style="color: #cc0000;">&quot;Contact&quot;</span>, <span style="color: #cc0000;">&quot;MailingList&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&lt;/ul&gt;</pre></div></div>

<p>I also added a helper to render the login/logout link in the navigation based on whether the request was authenticated.  The second level navigation was where the work came in. Essentially I wanted to render some of the actions from the current controller based on a attribute assigned to the action. The rending will then sort the list based on the sort order and perform security trimming.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>NavigationItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;About Us&quot;</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span>, GetIndexModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>NavigationItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Secured Page&quot;</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>, Authorize<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Admin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span>, GetIndexModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In order to do that I created a custom Attribute to store the link text and the order it should be in.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>AttributeUsage<span style="color: #008000;">&#40;</span>AttributeTargets<span style="color: #008000;">.</span><span style="color: #0000FF;">Method</span>, Inherited <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span>, AllowMultiple <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> NavigationItemAttribute <span style="color: #008000;">:</span> Attribute
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> NavigationItemAttribute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> text<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span>text, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NavigationItemAttribute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> text, <span style="color: #6666cc; font-weight: bold;">int</span> order<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Text <span style="color: #008000;">=</span> text<span style="color: #008000;">;</span>
        SortOrder <span style="color: #008000;">=</span> order<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Text <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> SortOrder <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>After that I needed a way to cache all this information in order to look it up later from my helpers. Populating this cache involves doing some basic reflection and type checking of controller actions. Then storing the results in a dictionary based on controller name as the key and a list of actions with the NavigationItemAttribute (stored as a ControllerNavigationItem, which is a POCO object to store data).  Since I only wanted to do this reflection once, I created a singleton to do the work.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> ControllerNavigationItemCollection
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> ControllerNavigationItemCollection _instance<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">object</span> padlock <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, IEnumerable<span style="color: #008000;">&lt;</span>ControllerNavigationItem<span style="color: #008000;">&gt;&gt;</span> Controllers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    ControllerNavigationItemCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Controllers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, IEnumerable<span style="color: #008000;">&lt;</span>ControllerNavigationItem<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        PopulateCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulateCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var asm <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var controllers <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> t <span style="color: #0600FF; font-weight: bold;">in</span> asm<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTypes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                           <span style="color: #0600FF; font-weight: bold;">where</span>
                               <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Controller<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsAssignableFrom</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
                               <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Controller<span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> t
                           <span style="color: #0600FF; font-weight: bold;">select</span> t<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        controllers<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">ForEach</span><span style="color: #008000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> Controllers<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, GetControllerNavItems<span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>ControllerNavigationItem<span style="color: #008000;">&gt;</span> GetControllerNavItems<span style="color: #008000;">&#40;</span>Type controllerType<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var controllerDescriptor <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ReflectedControllerDescriptor<span style="color: #008000;">&#40;</span>controllerType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        var actions <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> a <span style="color: #0600FF; font-weight: bold;">in</span> controllerDescriptor<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCanonicalActions</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                       let subNavAttr <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>NavigationItemAttribute<span style="color: #008000;">&#41;</span>a<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttributes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>NavigationItemAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                       let authorize <span style="color: #008000;">=</span> a<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttributes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>AuthorizeAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                       <span style="color: #0600FF; font-weight: bold;">where</span> a<span style="color: #008000;">.</span><span style="color: #0000FF;">IsDefined</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>NavigationItemAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span>
                       <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span> ControllerNavigationItem
                       <span style="color: #008000;">&#123;</span>
                           Action <span style="color: #008000;">=</span> a<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionName</span>,
                           Controller <span style="color: #008000;">=</span> a<span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerDescriptor</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerName</span>,
                           IsSecure <span style="color: #008000;">=</span> authorize <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span>,
                           SortOrder <span style="color: #008000;">=</span> subNavAttr<span style="color: #008000;">.</span><span style="color: #0000FF;">SortOrder</span>,
                           Text <span style="color: #008000;">=</span> subNavAttr<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span>
                       <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsEnumerable</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> actions<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> ControllerNavigationItemCollection Current
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span>padlock<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> _instance <span style="color: #008000;">??</span> <span style="color: #008000;">&#40;</span>_instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ControllerNavigationItemCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Finnaly to render the second level navigation I look up the list based on the current controller and loop over the items, only rendering the secure and authenticated items or the public items in the order defined by the attribute.</p>
<p>In hindsight, there are some things I could have made better or more features I could support. Currently it just checks to see if your logged in and doesn&#8217;t take into account any specific roles on the action.  Also it lacks support for MVC 2 Areas, but I don&#8217;t have any yet so I will cross that bridge when I get to it. This solution was simply an exercise for me in creating a singleton, using reflection to find controllers and actions, and to fill a common need in an ASP.Net MVC application.  Hope you found this interesting or useful.</p>
<p>Download the code <a href="http://gist.github.com/gists/606514/download">here</a> or check out the <a href="http://gist.github.com/606514">Gist</a> on <a href="http://github.com">Github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2010/10/01/simple-asp-net-mvc-navigation-menu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jMonthCalendar: Minor Release 1.2.0</title>
		<link>http://www.bytecyclist.com/2009/02/12/jmonthcalendar-minor-release-120/</link>
		<comments>http://www.bytecyclist.com/2009/02/12/jmonthcalendar-minor-release-120/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 19:04:52 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[jMonthCalendar]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=174</guid>
		<description><![CDATA[Wow, let me first start by saying it has been too long since I posted any updates on jMonthCalendar. I hope that the new release of jMonthCalendar 1.2.0 excites you as much as it does me. 1.2.0 sets the stage &#8230; <a href="http://www.bytecyclist.com/2009/02/12/jmonthcalendar-minor-release-120/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wow, let me first start by saying it has been too long since I posted any updates on jMonthCalendar.  I hope that the new release of jMonthCalendar 1.2.0 excites you as much as it does me.  1.2.0 sets the stage for some pretty cool stuff. Continue reading to see a short list of the new features and where this project is going next.<br />
<span id="more-174"></span><br />
On the short list of new features there is:</p>
<ul>
<li>Today Link (navigate back to today&#8217;s date)</li>
<li>Year navigation, jump to &#8217;10 or back to &#8217;08 in the active month</li>
<li>Ability to click a day cell or day link and fire your own custom logic (i.e. add event on that day)</li>
<li>JSON date parsing, parse what some from that event object if the calendar is un-aware.</li>
<li>Fixed in-line sizing, configurable by options conscruct</li>
<li>Deprecating Date property, replaced by StartDate</li>
<li>Added EndDate property to event object</li>
</ul>
<p>While not all of all of them seem flashy, the last three or four points make the calendar that much closer to having multi-day events and event overflow support.</p>
<p>I am happy to report that I am currently using the calendar on an ASP.Net MVC site on my development machine.Ã‚Â  I hope to have a new calendaring system ready soon.Ã‚Â  This should concern you only because when I find issues while developing and using it, I fix them and everyone benefits.Ã‚Â  In more detail my calendar does a JSON GetData call to a controller actions, which executes and returns JSON formatted events.Ã‚Â  I am using <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">James Newton-King&#8217;s JSON.Net</a> library to map and serialize my C# objects.Ã‚Â  I use the attributes on my properties to rename or exclude them from serialization.Ã‚Â  Very nice library and commend James for the work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2009/02/12/jmonthcalendar-minor-release-120/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC Release Canidate</title>
		<link>http://www.bytecyclist.com/2009/01/29/aspnet-mvc-release-canidate/</link>
		<comments>http://www.bytecyclist.com/2009/01/29/aspnet-mvc-release-canidate/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 17:54:49 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=159</guid>
		<description><![CDATA[If you didn&#8217;t know already, ASP.Net MVC framework is int he release candidate stages. Wahoo!!!. As per usual ScottGu over at Microsoft posted insanely detailed remarks on the new features. Check it out here.. Some notable improvements that I have &#8230; <a href="http://www.bytecyclist.com/2009/01/29/aspnet-mvc-release-canidate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you didn&#8217;t know already, ASP.Net MVC framework is int he release candidate stages. Wahoo!!!. As per usual ScottGu over at Microsoft posted insanely detailed remarks on the new features. <a href="http://weblogs.asp.net/scottgu/archive/2009/01/27/asp-net-mvc-1-0-release-candidate-now-available.aspx">Check it out here.</a>. Some notable improvements that I have used and like thus far include NO MORE CODE BEHIND FILES (this is my favorite), shortcut&#8217;s to switch between views and controllers, and &#8220;scaffold&#8221; like generation of views and controllers using the T4 template engine in Visual Studio.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2009/01/29/aspnet-mvc-release-canidate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC &#8220;Areas/Modules&#8221;</title>
		<link>http://www.bytecyclist.com/2008/12/03/aspnet-mvc-areasmodules/</link>
		<comments>http://www.bytecyclist.com/2008/12/03/aspnet-mvc-areasmodules/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 16:40:57 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=75</guid>
		<description><![CDATA[In this post Phil Haack explains how Areas can be accomplished in the ASP.Net MVC framework. I really like this idea of have Areas or Modules in ASP.Net MVC.Ã‚Â  I have been in the process of developing a Forum for &#8230; <a href="http://www.bytecyclist.com/2008/12/03/aspnet-mvc-areasmodules/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx" target="_blank">In this post Phil Haack explains how Areas can be accomplished in the ASP.Net MVC framework.</a></p>
<p>I really like this idea of have Areas or Modules in ASP.Net MVC.Ã‚Â  I have been in the process of developing a Forum for a website and came across this after I was complete.Ã‚Â  I thought that &#8220;yes it would be nice to use this same logic in another application&#8221;, and having an areas or module to move around would be quite nice.<br />
<span id="more-75"></span><br />
One thing I would expand upon is the idea of creating a self contained binary (dll) that can be referenced into the main project.Ã‚Â  This would mean that all the views and resources would have to be staticly defined in the binary, but could be easily re-styled with CSS.Ã‚Â  You&#8217;d probably also have to define a configuration element in your web.config that defines database connections and other information important/configurable to the Module/Area.</p>
<p>We&#8217;ll see how time permits for a prototype.Ã‚Â  Not something I <span style="text-decoration: underline;">need</span> now but is cool to think of.Ã‚Â  I wish the ASP.Net MVC guys/gals would include this functionality of segragated sections/areas/modules in a future release of ASP.Net MVC</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2008/12/03/aspnet-mvc-areasmodules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Frustrating IIS7 Configuration 500 Error</title>
		<link>http://www.bytecyclist.com/2008/09/22/frustrating-iis7-configuration-500-error/</link>
		<comments>http://www.bytecyclist.com/2008/09/22/frustrating-iis7-configuration-500-error/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 18:13:05 +0000</pubDate>
		<dc:creator>Kyle</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[IIS7]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.bytecyclist.com/?p=19</guid>
		<description><![CDATA[Recently I have been putting a lot of my evenings and weekends in to a re-architecting of a site I host and develop for some friends of mine Pert&#8217; Near Sandstone. The Entire site is built using ASP.Net and features &#8230; <a href="http://www.bytecyclist.com/2008/09/22/frustrating-iis7-configuration-500-error/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently I have been putting a lot of my evenings and weekends in to a re-architecting of a site I host and develop for some friends of mine <a title="Pert Near Sandstone" href="http://www.pertnearsandstone.com" target="_blank">Pert&#8217; Near Sandstone.</a> The Entire site is built using ASP.Net and features a standard (web forms) website front end (public facing) and an admin side also built using web forms.Ã‚Â  I liked this at the time, but it soon became very difficult to debug, maintain and add features to.</p>
<p>The biggest factor in deciding the re-architect the site at the time was the lack of interoperability in the Admin side/application.Ã‚Â  It worked as intended about 60% of the time (60% of the time it works every time).Ã‚Â  The web forms admin side required a lot of code that was spread out over numerous code behind files, making it difficult to maintain.Ã‚Â  Not to mention similar logic was being duplicated, violating the DRY principal (don&#8217;t repeat yourself).</p>
<p>Around the same time I had just finished my exploration of the new ASP.Net MVC (model-view-controller) model and had fallen in love with web development all over again.Ã‚Â  I had been in the process of creating some sample applications with it.Ã‚Â  I even started testing how it deployed on various web hosting platforms.Ã‚Â  I discovered that the MVC pattern DID work on IIS6 and IIS7.Ã‚Â  The URL&#8217;s on IIS6 required special mapping (either adding .aspx or .mvc to map to the aspnet framework), I didn&#8217;t like that so I found a host that offered IIS7.Ã‚Â  The URL&#8217;s are now clean and to my liking.Ã‚Â  MVC makes sense, it creates clean separation of UI, business logic and data models. and will be an architecture of choice on ASP.Net.</p>
<p>So to make my long story short and to the point.Ã‚Â  I ended up re-architecting the Admin (once web forms side) of my site/project into a new MVC project in my solution.Ã‚Â  I started bringing over functionality one peice at a time, while reworking the underlying business logic and data access logic into various providers and repositories.Ã‚Â  I had everything working perfectly for a phase one deployment to the web host.Ã‚Â  I had 2 projects to deploy, one public facing site (web forms) and one admin MVC site.Ã‚Â  I set up the web host to use a virtual directory for the the MVC site, configured my connection strings as:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connectionStrings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- PRD --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;PertNear.Data.Properties.Settings.PertNearSandstoneConnectionString&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">connectionString</span>=<span style="color: #ff0000;">&quot;server=secureserver.net;uid=user;pwd=pass;database=db&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">providerName</span>=<span style="color: #ff0000;">&quot;System.Data.SqlClient&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;PertNear.Membership&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">connectionString</span>=<span style="color: #ff0000;">&quot;server=secureserver.net;uid=user;pwd=pass;database=db&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">providerName</span>=<span style="color: #ff0000;">&quot;System.Data.SqlClient&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">    &lt;!--  DEV</span>
<span style="color: #808080; font-style: italic;">    &lt;add name=&quot;PertNear.Data.Properties.Settings.PertNearSandstoneConnectionString&quot;</span>
<span style="color: #808080; font-style: italic;">connectionString=&quot;Data Source=localhost;Initial Catalog=DB;Integrated Security=True&quot;</span>
<span style="color: #808080; font-style: italic;">providerName=&quot;System.Data.SqlClient&quot; /&gt;</span>
<span style="color: #808080; font-style: italic;">    &lt;add name=&quot;PertNear.Membership&quot;</span>
<span style="color: #808080; font-style: italic;">connectionString=&quot;Data Source=localhost;Initial Catalog=DB;Integrated Security=True&quot;</span>
<span style="color: #808080; font-style: italic;">providerName=&quot;System.Data.SqlClient&quot; /&gt;</span>
<span style="color: #808080; font-style: italic;">    --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connectionStrings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I uploaded everything to the virtual directory and tried to access the new applications.  The public facing side (web forms) worked cause it had a separate config file, the admin side through the generic IIS7 internal server error with no messages.  I looked everywhere to find a solution.  Many sources said to add a wrapping element around the parent web.config so that it&#8217;s settings don&#8217;t conflict with the virtual directories (inheritance in IIS7 config files). I spent a week plus on this issue until I ran the config file through an XML validator.  As you can see I had a rouge comment tag in the config.  Had I known that IIS7 was first validating my config file I might have gotten an error along the lines of &#8220;invalid xml&#8221; instead of generic 500 error.</p>
<p>Reader be warned, be sure the validate the web.config file before uploading.  And check the config file before you make any drastic changes like I did (I moved the MVC project into the public facing site to now have just one project to deploy, thinking that might solve the issue).  One point worth mentioning, I had edited my published web.config outside of VS2008 in a text editor, hopefully VS would have caught that error for me had I known.</p>
<p>Good luck, happy coding and avoid the frustration I had.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytecyclist.com/2008/09/22/frustrating-iis7-configuration-500-error/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

