Creating a ContentPage controller in ASP.NET MVC

This tutorial assumes you have added the Visual Studio item templates. How to do this is documented in Setting Up Your Environment

Right-click on the project, select add and select N2 Content Controller.

The item template includes a content page class. Take note of these traits shared by most content items:

  1. It’s attributed by [PageDefinition]
  2. It inherits from N2.ContentItem

It’s enriched by attributes with an editable title, name (url segment), and free text

A content controller is also generated. It shares some traits in common with most content controllers:

  1. It [Controls] a type of content item (the one that was generated).
  2. It inherits from ContentController<T>. The generic argument gives strongly typed access to CurrentItem
  3. It has an Index action.

There is no view yet, but this is easy enough to generate. Right-click in the Index action method, and Add View…

Create a strongly typed view by setting the view data class to the generated model class. The view uses all regions from the master page. Usually MainContent is enough and the others can be removed.

Tweaking the view a bit will reveal content managed from the UI:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.DisplayContent("Title") %>
    <%= Html.DisplayContent("Text") %>
</asp:Content>

A quick compile (F6 in visual studio) makes the page available in the management UI.