This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

The new Sitecore MVC solution allows you to associate a controller with an item, below you can see that I have attached a controller to my Home item:
My controller is very simple:
    public class TestController : SitecoreController
    {      
        public override System.Web.Mvc.ActionResult Index()
        {
            return View("Index");
        }
    }
And the view:
@inherits System.Web.Mvc.WebViewPage
@using System.Web.Mvc.Html
<html>
<body>
    <h1>
        Test Index Page</h1>
</body>
</htm>
I have also not set a layout or any controls in the items presentation layer. However when I load the home page I get the following error:
That is odd, we have setup everything correctly but an error is thrown. This is actually a bug in the Sitecore implementation of the Item Controller. This bug can easily be fixed. The Item Controllers are called by the class Sitecore.Mvc.Pipelines.Request.CreateController.CreateItemController which is configured in the Sitecore.Mvc.config, all we need to do is create another class that replaces it:
   public class CreateItemController : CreateControllerProcessor
    {

        public override void Process(CreateControllerArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            if (args.Result != null)
                return;
            args.Result = this.CreateController(args);
        }

        protected virtual IController CreateController(CreateControllerArgs args)
        {
            Debug.ArgumentNotNull((object)args, "args");
            Item obj = args.PageContext.Item;
            if (obj == null)
                return (IController)null;
            else
                return this.CreateControllerFromItem(obj, args);
        }


        protected virtual IController CreateControllerFromItem(Item item, CreateControllerArgs args)
        {
            if (!string.IsNullOrEmpty(item["_Controller"]))
            {
              RequestContext requestContext = PageContext.Current.RequestContext;
              requestContext.RouteData.Values["controller"] = item["__Controller"];
            }
            
            return new ControllerLocator().GetController(item["__Controller"], item["__Controller Action"]);
            
        }
      
    }
The only difference in this class from the original class is the addition of these four lines:
            if (!string.IsNullOrEmpty(item["_Controller"]))
            {
              RequestContext requestContext = PageContext.Current.RequestContext;
              requestContext.RouteData.Values["controller"] = item["__Controller"];
            }
Next add an include config file with the following:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.createController>
        <processor type="Sitecore.Mvc.Pipelines.Request.CreateController.CreateItemController, Sitecore.Mvc">
          <patch:attribute name="type">Demo.Pipelines.MvcRequestBegin.CreateItemController, Demo</patch:attribute>
        </processor>
      </mvc.createController>
    </pipelines>
  </sitecore>
</configuration>
If we compile the app and request the page again we should get the view returned correctly: