Profile picture Schedule a Meeting
c a n d l a n d . n e t

Setup N2Cms on an Asp.Net MVC

Dusty Candland | |

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.

Global.asax

  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)

        {

            routes.IgnoreRoute("{resource}.axd/{pathInfo}");

            routes.IgnoreRoute("{resource}.ashx/{pathInfo}");

 

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

            routes.Add(new ContentRoute(engine));

 

            routes.MapRoute(

                "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()

        {

            EventBroker.Instance.Attach(this);

            base.Init();

        }

    }

Web.config

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"/>

    </sectionGroup>

Change the connection string name to N2CMS

Add the n2 section.

<n2>

    <!— 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"/>

      </web>

    </host>

    <engine>

      <assemblies>

        <!— These are only needed for medium trust

       

       

        —>

      </assemblies>

    </engine>

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

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

    <edit>

      <installer checkInstallationStatus="true"/>

    </edit>

  </n2>

Add expressionBuilders to the system.web compilation section.

 

      <expressionBuilders>

        <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"/>

      </expressionBuilders>

Change the forms authentication section.

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

      </forms>

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" verb="" type="N2.Web.AjaxRequestHandler, N2" />

Add a handler to the system.webServer handlers section.

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

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.

Models\AbstractPage.cs

using N2;

using N2.Details;

 

namespace Red27.Site.Models

{

    [WithEditableTitle, WithEditableName]

    public class AbstractPage : ContentItem, INode

    {

        public string PreviewUrl

        {

            get { return Url; }

        }

    }

}

Models\ContentPage.cs

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

        }

    }

}

Controllers\ContentController.cs

using N2.Web;

using Red27.Site.Models;

using N2.Web.Mvc;

 

namespace Red27.Site.Controllers

{

    [Controls(typeof(AbstractPage))]

    public class ContentController : ContentController<AbstractPage>

    {

    }

}

Views\Content\DefaultView.aspx

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

    Inherits="ViewPage" %>

 

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

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

    <h1>

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

    <%= Model.Text %>

</asp:Content>

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.

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: