Page Types

Content Type Development

The N2 programming model requires each type of content to have a class that is compiled into the application. Instances of this class are later used from a template, controller or view.

Consider the following content class:

[PageDefinition("TutorialPage")]
[WithEditableTitle, WithEditableName]
public class TutorialPage : ContentItem
{
     [EditableFreeTextArea("Text", 100)]
     public virtual string Text
     {
           get { return (string)(GetDetail("Text") ?? string.Empty); }
           set { SetDetail("Text", value, string.Empty); }
     }
}

Ever since N2 CMS 2.1 it’s possible to get rid of the bulky getters and setters:

[PageDefinition("TutorialPage")]
[WithEditableTitle, WithEditableName]
public class TutorialPage : ContentItem
{
     [EditableFreeTextArea("Text", 100)]
     public virtual string Text { get; set; }
}

A common pattern is using an abstract base class for re-use across multiple types:

[WithEditableTitle, WithEditableName]
public abstract class ContentPageBase : ContentItem
{
     [EditableFreeTextArea("Text", 100)]
     public virtual string Text { get; set; }
}
 
[PageDefinition("Tutorial Page")]
public class TutorialPage : ContentPageBase
{
}
 
[PageDefinition("Example Page")]
public class ExamplePage : ContentPageBase
{
}

Both TutorialPage and ExamplePage will have an editable title, name and text.

When developing an ASP.NET MVC application the content item doesn’t know which template will use it (the controller defines which content item it controls). When using WebForms however, TemplateUrl is used:

/// <summary>
/// This is WebForms' style of content item declaration.
/// </summary>
[PageDefinition("WebForm Page",
     TemplateUrl = "~/Path/To/My/Template.aspx")]
public class WebFormPage : ContentPageBase
{
}