The following are my notes on setting up a n2cms site starting with a default MVC project and copying in the needed stuff from the Example_Mvc sample from the n2cms site.

Starting with a new MVC site. Reference the dll’s in the \Example_Mvc\Mvc\wwwroot\Bin folder. I copied them to an externals folder under my solution and referenced them there.


  1. Add IEngine parameter to the RegisterRoutes method.

  2. Add a new ContentRoute to the RouteCollection.

  3. Add an ignore for .ashx routes.

  4. Initialize the N2 engine and pass to the RegisterRoutes in the Application_Start()

  5. Override Init and attach this to the EventRroker instance.


    public class MvcApplication : System.Web.HttpApplication


        public static void RegisterRoutes(RouteCollection routes, IEngine engine)





            // This route detects content item paths and executes their controller

            routes.Add(new ContentRoute(engine));



                "Default",                                              // Route name

                "{controller}/{action}/{id}",                           // URL with parameters

                new { controller = "Home", action = "Index", id = ""// Parameter defaults





        protected void Application_Start()


            // normally the engine is initialized by the initializer module but it can also be initialized this programmatically

            // since we attach programmatically we need to associate the event broker with a http application

            IEngine engine = N2.Context.Initialize(false);


            RegisterRoutes(RouteTable.Routes, engine);



        public override void Init()








Add the n2 config sections.


    <sectionGroup name="n2" type="N2.Configuration.SectionGroup, N2">

      <section name="host" type="N2.Configuration.HostSection, N2" requirePermission="false"/>

      <section name="engine" type="N2.Configuration.EngineSection, N2" requirePermission="false"/>

      <section name="database" type="N2.Configuration.DatabaseSection, N2" requirePermission="false"/>

      <section name="edit" type="N2.Configuration.EditSection, N2" requirePermission="false"/>



Change the connection string name to N2CMS

Add the n2 section.


    <!— If you install a database from scrach you’ll need to insert some required pages. This can be done by the web based installer located at http://yoursite/install/edit —>

    <host rootID="1" startPageID="1">

      <web extension="" rewrite="None">

        <urls enableCaching="false"/>





        <!— These are only needed for medium trust






    <!— Other flavours: SqlServer2005, SqlServer2000, MySql, SqLite —>

    <database connectionStringName="N2CMS" flavour="SqlServer2005"/>


      <installer checkInstallationStatus="true"/>




Add expressionBuilders to the system.web compilation section.



        <add expressionPrefix="CurrentItem" type="N2.Web.Compilation.CurrentItemExpressionBuilder, N2"/>

        <add expressionPrefix="CurrentPage" type="N2.Web.Compilation.CurrentPageExpressionBuilder, N2"/>

        <add expressionPrefix="Code" type="N2.Web.Compilation.CodeExpressionBuilder, N2"/>

        <add expressionPrefix="StartPage" type="N2.Web.Compilation.StartPageExpressionBuilder, N2"/>

        <add expressionPrefix="HasValue" type="N2.Web.Compilation.HasValueExpressionBuilder, N2"/>



Change the forms authentication section.

      <forms loginUrl="edit/login.aspx" protection="All" timeout="30000" path="/">



Add the n2 tag prefix to the pages controls section.

        <add tagPrefix="n2" assembly="N2" namespace="N2.Web.UI.WebControls"/>


Add the n2.ashx httpHandler

      <add path=".n2.ashx</span>" verb="</em>" type="N2.Web.AjaxRequestHandler, N2" /></p> </p></div>

Add a handler to the system.webServer handlers section.

      <add name="n2.ajax" path=".n2.ashx</span>" verb="</em>" type="N2.Web.AjaxRequestHandler, N2" /></p> </p></div>

The Start and Root Controller

Now we need to add a a ContentController class to the Controllers folder. And an AbstractPage and ContentPage to the Models folder. And a Content folder and DefaultView.aspx page the Views\Content folder. This will become the start and root nodes for the N2 CMS.


using N2;

using N2.Details;


namespace Red27.Site.Models


    [WithEditableTitle, WithEditableName]

    public class AbstractPage : ContentItem, INode


        public string PreviewUrl


            get { return Url; }






using N2;

using N2.Details;


namespace Red27.Site.Models


    [Definition("Content Page", Installer = N2.Installation.InstallerHint.PreferredStartPage)]

    public class ContentPage : AbstractPage


        [EditableFreeTextArea("Text", 100)]

        public virtual string Text


            get { return (string)(GetDetail("Text") ?? string.Empty); }

            set { SetDetail("Text", value, string.Empty); }



        public override string TemplateUrl


            get { return "/Views/Content/DefaultView.aspx"; }






using N2.Web;

using Red27.Site.Models;

using N2.Web.Mvc;


namespace Red27.Site.Controllers



    public class ContentController : ContentController<AbstractPage>






<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"

    Inherits="ViewPage"</span> %> </p>


<%@ Import Namespace="Red27.Site.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">


        <%= Model.Title %></h1>

    <%= Model.Text %>


</p> </div>

Initial DB Setup

Copy the Edit directory from Example_Mvc\Mvc\wwwroot to the root of your web project. This is the code that handles setup and content management and shouldn’t need to be modified. Add a new mdf database in the app_data directory or point the connection string to some other DB.

Now the site can be compiled and run. This should bring up the /edit/install page to create the db tables and insert the root node value. You may need to stop the development server and restart it to see your ContentPage in the root and start node drop downs. Once this is done you should be able to edit the content of your Start Page!

What’s Next?

A few more details need to be handled, like authentication, some N2 setup on the Site.Master page, and some clean up of unused pages from the MVC project setup.