harriyott.com

13 January 2010

Adding SEO fields to N2 MVC example

I've been using (a copy of) the MVC example from the N2 trunk. By default, there is no SEO tab. To add this, copy SEODefinitionAppender.cs and TitleAndMetaTagApplyer.cs from the N2.Templates.Mvc project (Services folder) to the example project. Then add the line
engine.AddComponent("n2.templates.seo.definitions",
typeof(SEODefinitionAppender));
to the end of Application_Start in global.asax.cs.
The SEO tab and fields should now appear on the edit page.

The next step is to get the SEO fields into the master page. If there's no code-behind class for the master page, then add one, and derive the class like so:
Site : ViewMasterPage<IItemContainer>, IItemContainer<ContentItem>

Add the following to the class:
protected override void OnInit(EventArgs e)
{
_titleApplyer = new TitleAndMetaTagApplyer(this.Page, Model.CurrentItem);
}

public ContentItem CurrentItem
{
get
{
return Model.CurrentItem;
}
}
Derive AbstractPage from IItemContainer, and implement the CurrentItem getter by returning "this". Make sure all view pages' view data is derived from AbstractPage or IItemContainer.

6 January 2010

Setting up N2, the open source, ASP.NET (with optional MVC) CMS

Here's what I did to get started with N2, the best CMS I've used to date. I've not set up one of these for a while, so I thought I'd document the process:

  1. Check out the latest source from the subversion trunk - http://code.google.com/p/n2cms/source/checkout
  2. Ran Prepare_Dependencies-vs2008.bat in the root folder
  3. Ran Build_Everything-vs2008.bat, also in the root
  4. Made a copy of examples/Mvc to use as a new project
  5. Loaded the solution in VS2008
  6. Created the database, and ran the script
  7. Built and ran the project
  8. Navigated to /edit/install to see how I was doing


Not very well. I got a "No component for supporting the service N2.Web.Mvc.IControllerMapper was found" error. Google told me very little, except that someone else had the same problem and posted it on the forum. I posted too, and within half an hour, Steve Mason (who I worked with at Cubeworks, and is a contributor to N2) had replied asking for a stack trace. I provided one, and Steve provided the fix. Back on track.

I then checked /edit/install again, which prompted me to upgrade to version 2 of the database, by simply clicking a button. It then told me to add a root page, which I did. Now I'm up and running.

After copying a page or two, I kept getting a StackOverflowException thrown. Thanks to Joel and Jeff, these are harder to google for, but eventually I worked out that I needed to override TemplateUrl in the Model class (derived from AbstractPage).