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);
}

The code is below, but it can also be downloaded here and found on Github here.

using System;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
namespace com.bytecyclist
{
    public class JsonNetResult : ActionResult
    {
        public Encoding ContentEncoding { get; set; }
        public string ContentType { get; set; }
        public object Data { get; set; }
 
        public JsonSerializerSettings SerializerSettings { get; set; }
        public Formatting Formatting { get; set; }
 
        public JsonNetResult()
        {
            SerializerSettings = new JsonSerializerSettings();
 
            SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
            Formatting = Formatting.None;
        }
 
        public JsonNetResult(object data)
            : this()
        {
            Data = data;
        }
 
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
 
            var response = context.HttpContext.Response;
            response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
 
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
 
            if (Data == null) return;
            var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
 
            var serializer = JsonSerializer.Create(SerializerSettings);
            serializer.Serialize(writer, Data);
            writer.Flush();
        }
    }
}

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() %>
</ul>
 
<div class="clearboth"></div>
 
<ul id="subnavigation">
    <%= Html.SubNavListItems() %>
    <%= Html.NavItem("Join Our Mailing List!", "Contact", "MailingList") %>
</ul>

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.

public sealed class ControllerNavigationItemCollection
{
    static ControllerNavigationItemCollection _instance;
    static readonly object padlock = new object();
 
    public IDictionary<string, IEnumerable<ControllerNavigationItem>> Controllers { get; private set; }
 
    ControllerNavigationItemCollection()
    {
        Controllers = new Dictionary<string, IEnumerable<ControllerNavigationItem>>();
        PopulateCollection();
    }
 
    private void PopulateCollection()
    {
        var asm = Assembly.GetExecutingAssembly();
        var controllers = (from t in asm.GetTypes()
                           where
                               typeof(Controller).IsAssignableFrom(t) &&
                               typeof(Controller) != t
                           select t).ToList();
 
        controllers.ForEach(t => Controllers.Add(t.Name, GetControllerNavItems(t)));
    }
 
    private static IEnumerable<ControllerNavigationItem> GetControllerNavItems(Type controllerType)
    {
        var controllerDescriptor = new ReflectedControllerDescriptor(controllerType);
 
        var actions = (from a in controllerDescriptor.GetCanonicalActions()
                       let subNavAttr = (NavigationItemAttribute)a.GetCustomAttributes(typeof(NavigationItemAttribute), false).SingleOrDefault()
                       let authorize = a.GetCustomAttributes(typeof(AuthorizeAttribute), false).SingleOrDefault()
                       where a.IsDefined(typeof(NavigationItemAttribute), false)
                       select new ControllerNavigationItem
                       {
                           Action = a.ActionName,
                           Controller = a.ControllerDescriptor.ControllerName,
                           IsSecure = authorize != null,
                           SortOrder = subNavAttr.SortOrder,
                           Text = subNavAttr.Text
                       }).AsEnumerable();
 
        return actions;
    }
 
    public static ControllerNavigationItemCollection Current
    {
        get
        {
            lock (padlock)
            {
                return _instance ?? (_instance = new ControllerNavigationItemCollection());
            }
        }
    }
}

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.

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

ASP.Net MVC Release Canidate

If you didn’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 used and like thus far include NO MORE CODE BEHIND FILES (this is my favorite), shortcut’s to switch between views and controllers, and “scaffold” like generation of views and controllers using the T4 template engine in Visual Studio.

ASP.Net MVC “Areas/Modules”

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 a website and came across this after I was complete.  I thought that “yes it would be nice to use this same logic in another application”, and having an areas or module to move around would be quite nice.
Continue reading

Frustrating IIS7 Configuration 500 Error

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’ Near Sandstone. 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.

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’t repeat yourself).

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’s on IIS6 required special mapping (either adding .aspx or .mvc to map to the aspnet framework), I didn’t like that so I found a host that offered IIS7.  The URL’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.

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:

    <connectionStrings>
    <!-- PRD -->
    <add name="PertNear.Data.Properties.Settings.PertNearSandstoneConnectionString"
connectionString="server=secureserver.net;uid=user;pwd=pass;database=db"
providerName="System.Data.SqlClient"/>
    <add name="PertNear.Membership"
connectionString="server=secureserver.net;uid=user;pwd=pass;database=db"
providerName="System.Data.SqlClient" />
 
    <!--
    <!--  DEV
    <add name="PertNear.Data.Properties.Settings.PertNearSandstoneConnectionString"
connectionString="Data Source=localhost;Initial Catalog=DB;Integrated Security=True"
providerName="System.Data.SqlClient" />
    <add name="PertNear.Membership"
connectionString="Data Source=localhost;Initial Catalog=DB;Integrated Security=True"
providerName="System.Data.SqlClient" />
    -->
    </connectionStrings>

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’s settings don’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 “invalid xml” instead of generic 500 error.

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.

Good luck, happy coding and avoid the frustration I had.