Is This Thing Still On

Wow, it has been a long time since the last post. Since I don't think anyone is reading this and late I wanted to make sure the tires were kicked. I updated the theme tonight to use the wonderful template from poole. I like it so far, much less to maintain.

Like most bloggers I have great dreams of filling this up with code samples, musing, and deep thoughts. Here is to hoping I don't waste time and get words on the screen. Whiskey in hand, lets make it happen. Be back soon!

Smart Windows of the Future

This is awesome, I want one of these in my house! Can you imagine a weather overlay on it or watching the news. The blind feature is by far the coolest, minimal design saves me from curtains.

Welcome To My New Octopress Blog

Well, this is the obligatory first post of any new blog. Recently I have been itching for an excuse to start a new project. Setting up a blog seemed liked a good use of time. In the process I learned how to once again setup a Ubuntu Server, host private git repositories, use git with multiple remotes, and install and configure various linux packages.

Who am I?

My Name is Kyle LeNeau, I'm a software developer/engineer/architect living the Minneapolis, MN area. I enjoy learning and experiencing new technologies and I can usually pick something up rather quickly. My main areas of focus recently have been in Web Application Development and Mobile Development. Coding is my passion and I have been doing it professionally for the last 6+ years.

What's Octopress?

As the title of this post states, this is my new blog powered by Octopress which happens to be an open source project hosted over at github. The framework is very different than other blogging engines like Wordpress since it's output is static html files instead of being dynamically rendered from php and a database. Static sites were the original websites back in the day. If you wanted to host something online you needed to know Html at a minimum. Today, we have higher level languages that let us "speak" more naturally and code generators to take what we "speak" and output something else. This is what Octopress does, it takes files written in the Markdown language and processes them through a series of templates using Ruby to output a directory of Html files.

Why Static Content?

Bottom line, static sites are fast. There is no processing or scripting time required by the server to output the Html to the browser. The server receives a request to an html file, finds it on the server, and then opens it for reading back to the browser. The great thing about this is that the server doesn't need to be anything spectacular. Also, with the use of caching and web server choice you can really hit the sweet spot when it comes to performance and out of pocket cost.

As I see it, this blog is a back to basic for me. I would like to find my voice in the world and start sharing my various experiences so that one day someone will read this and find it useful. I am always consuming content on the internet, now is my chance to give something back and fill in some gaps I have encountered so that others don't have to.

Json.Net Action Result with Asp.Net MVC

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's Json.Net library. 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.

You might be thinking, why not just use the one that comes with .Net. It'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 Json.Net library. Enjoy!

Example usage in any controller action:

public ActionResult List(int Year, int Month)
    if (Year == 0)
        Year = DateTime.Now.Year;

    if (Month == 0)
        Month = DateTime.Now.Month;

    var calendarMonth = _calendarService.GetCalendarMonth(Year, Month, !this.IsAdmin());

    return new JsonNetResult(calendarMonth);

Simple Asp.Net MVC Navigation Menu

Well it'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'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.

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 codeplex called ASP.NET MVC SiteMap Provider, 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).

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.

<ul id="navigation">
    <%= Html.NavItem("Home")%>
    <%= Html.NavItem("Calendar")%>
    <%= Html.NavItem("Media")%>
    <%= Html.NavItem("Messages", "Forum", "Index")%>
    <%= Html.NavItem("Goods", "Catalog", "Index")%>
    <%= Html.NavItem("Links", "Link", "Index")%>
    <%= Html.NavItem("Contact")%>
    <%= Html.LoginStatusNavItem() %>

<div class="clearboth"></div>

<ul id="subnavigation">
    <%= Html.SubNavListItems() %>
    <%= Html.NavItem("Join Our Mailing List!", "Contact", "MailingList") %>

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.

[NavigationItem("About Us", 0)]
public ActionResult Index()
    return View("Index", GetIndexModel());

[NavigationItem("Secured Page", 0), Authorize]
public ActionResult Admin()
    return View("Index", GetIndexModel());

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

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class NavigationItemAttribute : Attribute
    public NavigationItemAttribute(string text) : this(text, 0) { }

    public NavigationItemAttribute(string text, int order)
        Text = text;
        SortOrder = order;

    public string Text { get; private set; }
    public int SortOrder { get; set; }

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.

Finally 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.

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't take into account any specific roles on the action. Also it lacks support for MVC 2 Areas, but I don'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.

Download the code here or check out the Gist on Github