N2 Developer Reference

CMS Framework vs. Templates

The framework is represented by N2.dll, N2.Security.dll and N2.Management.dll as well as all files below the /N2/ folder. These reflect upon the application to create a UI where the site’s structure and content is managed. The framework provides tools the application can use to create a navigable web site.

The application references N2.dll and uses API:s in the N2 namespace to model content items so they can be managed from the management UI. The application contains the external shape of the site including master pages, style sheets and application logic. The application is developed with ASP.NET MVC or WebForms, either way they use the same version of the N2 CMS framework.

Web Application Architecture

The N2 framework has only one requirement on the application. There must be a class inheriting from N2.ContentItem. Instances of this class represent the pages managed in the UI. It’s a good idea to use multiple classes for different kinds of pages, and use inheritance to share common properties.

Each content class is related to one or more ASPX templates, or views which display the user managed content in a shared layout. The framework provides base classes and controls to simplify placing content, creating navigation and more.

When a page instance is created in the UI it is assigned a name which gives this page an URL from which it can be accessed. Assuming the start page has the path “/”, a page named “hello” will be accessible from the path “/hello/”. The framework is responsible for mapping this logical path to the ASPX template or view.

CMS Framework Architecture

The N2 CMS Framework uses Inversion of Control to compose all of the CMS’ functionality. During initialization a set of services are constructed and exposed through a singleton context located at N2.Context.Current. Many helper methods such as N2.Find.Items access this context to do their job.

Most of the interaction with N2 CMS is done through inheritance of classes or usage of attributes. During the initialization phase N2 analyzes the application and learns how to connect the content in the database with the application code.

It’s also possible to inject or replace services in N2 to bend the CMS’ behavior beyond recognition.

WebForms vs. MVC

Regardless of application technology content items are defined using classes and instances of these are created and organized though a management UI. Each instance is given a name which is used to construct the page’s logical URL.

In the WebForms application content classes define a path to an ASPX template which is used to render that particular type of item. When a page instance’s logical URL is accessed the framework executes this template.

In the MVC application a controller defines what types of content item it controls. When a page instance of this type is accessed the control is executed and it determines what action to perform (e.g. render a view).

Pages that are outside a start page, e.g. pages in trash can also be accessed but not via a friendly url.

Pages handled by an MVC controller are accessed via an url that corresponds to “/{controller}/{action}/?page={id}”.

Pages that are handled by a webforms page are accessible via “/{path-to-aspx-template}?page={id}”.

Routing to content with ASP.NET MVC

An example for an application using the ContentRoute and MVC. Model:

[PageDefinition]
public class ContentPage : PageModelBase
{
}

Controller:

[Controls(typeof(Models.ContentPage))]
public class ContentPagesController : ContentController<Models.ContentPage>
{
}

 And the following incoming URL:

/news/n2-cms-2-1-1-released

This URL is based on each page’s name and the names of the ancestors leading up to the start page. In this case the page “N2 CMS 2.1.1 released” is placed below the page “News” which is directly below the start page.

When a request arrives routes are asked in order if they can handle it. Since the URL maps to a content item the ContentRoute will respond with route data containing controller and referencing to the item.

Path segments immediately after a path leading to a controller are mapped as actions if there is a corresponding method on the controller. E.g. if there was an action “Comment” on the ContentPages controller the following url would lead to that action:

/news/n2-cms-2-1-1-released/comment

The following URL would also lead to this item and would be generated for pages that aren’t within a start page (e.g. if they have been thrown in the trash):

/ContentPages/Index/?page=123