[Resolved] Plug N2 into an existing ASP.net web site

Topics: Developer Forum
Dec 24, 2008 at 11:42 AM
Hi all,

I'm trying to plug N2 onto an already existing ASP.NET web site, I followed the steps describes here : http://n2cms.com/Documentation/Content-enabling%20an%20existing%20site.aspx
I rewrite my web.config, create tables, add Root and Start nodes, create a template page and the content class, everything is fine.
But when I go to the Edit page of N2 and Create a new TextPage, IIS is looking for a real page who do not exist so it return a 404 error.
May I miss something in the steps ? also, I don't understand how the Template and the content class works ?

If someone can help me, that will be great.
Thanks
Dec 24, 2008 at 1:04 PM
Edited Dec 24, 2008 at 1:15 PM
1. It's critical to have a <host/> section of your web.config pointing at a correct root node ID (and start page ID, unless it's the same as root):
...
 <host rootID="1" startPageID="2" multipleSites="false">
...
2. Must be sure that N2 initialization HTTP module is wired up:
a) IIS 6 & IIS 7 in "classic" mode
...
<system.web>
<httpModules>
<add name="n2.initializer" type="N2.Web.InitializerModule, N2"/>
...
b) IIS 7, by default:
...
<system.webServer>
<modules>
<add name="n2.initializer" type="N2.Web.InitializerModule, N2"/>
...

To understand how Templates and ContentItem classes work, think of them as a special MVC pattern implementation:
initially, someone (N2 initializer, a super contoller) makes a decision that a particular URL should be served by a certain controller
controller decides that a particular model should be rendered by a particular view.

Presently, in N2, responsibilities of controller and model are take by ContentItem. Responsibilities of view are performed by a usual .aspx pages.

 Namely, N2 roughly performs these steps:
0. Suppose that the incoming URL is http://myhost/mysite/part1/part2/part3.aspx and your site root ( ~/ ) lives at http://myhost/mysite/
1. N2 takes a starting node ID from a web.config and assumes it to be a site root, that is: ~/
2. Parses incoming URL part by part, and on each step it "asks" a current node either of two questions:
2.1 If there're still any unparsed URL parts remaining, then it asks "give me your child with a name partN ", makes this child a current node and proceed recursively (so, in effect, it walks a hierarchy of nodes: StartPage>part1>part2>part3)..
2.2 If URL is over then N2 asks the last node (part3, in our case): "what's your template?"
3. N2 takes the template from the last node and simply renders it as a usual ASP.Net .aspx, giving it as a bonus that last node as a CurrentItem property.

Though, this example was oversimplified, hope it gives some clue..
Dec 24, 2008 at 1:28 PM
Edited Dec 24, 2008 at 1:51 PM
Thank you for reply, I updated my web.config and it seem to be better.

In your example you said N2 ask the Template name to Part3, but how can I configure which template is using by Part3 ?
Now I have created a simple page in N2 but it does not have a template, I would like to use my custom template.
Dec 24, 2008 at 5:55 PM
Edited Dec 24, 2008 at 5:58 PM
Aha, i think we should define the term "template" first. Here's how it goes:

ContentItem (aka "controller", aka "model", aka "entity", etc) is a pice of data
An ordinary ASP.Net page (.aspx) is a piece of GUI and is called template.
ContentItem (i.e.: you, as a developer) is free to choose any .aspx as a template by means of:
a) overriding string TemplateUrl { get; } property (old way)
b) decorating ContentItem with [N2.Web.Template("~/MyPage.aspx")] attribute (new shiny way).

Once again, a template can be any .aspx you choose, be it newly developed or existing one, from your already exising application.

The main difference in request processing pipe-line between a "core" ASP.Net and N2 is in the way they associate incoming URLs (requests) and .aspx pages (responses).
1. In ASP.Net the workflow is very simple: URL => .aspx (you give an URL=> ASP.Net will find an exact page for you)
2. In N2 it's a little bit longer: URL => ContentItem => .aspx (you give an URL => N2 will find a Content Item for it => Content Item will name an .aspx page)

So, it makes sense to rethink you application in terms of Entities first, and then associate your existing pages to these entities as templates.
Dec 29, 2008 at 12:57 PM
Thank you so much for your help, it's working now and I understand better N2.
Now I have created my first Template (a text page) but I can't delete it : I receive a red "Permission denied" on top of the delete page, any idea ?
Coordinator
Dec 30, 2008 at 9:20 PM
How are you deleting the page? Are you by any chance deleting it as an anonymous user? In that case the problem might be that the page is moved to the trash can which is not accessible to anonymous users.
Jan 6, 2009 at 1:30 PM
hi libardo,
When I plugged N2 into my existing web site, I only seen Root and Start page.
After esteewhy's help I was able to add template and add a Text Page.
But when I try to delete this page from N2 Edit interface, I receive a red Permission denied message in top of page.
But I've update every web.config file to allow my admin user, and I can't see where the Trash page is.

Coordinator
Jan 6, 2009 at 9:06 PM
Can you put this code somewhere:

The user is editor: <%= N2.Context.Current.SecurityManager.IsEditor(Page.User) %>

Can you post some screenshots and the n2 section in web.config along with the result from above? It might help in figuring out what's going on.

Jan 7, 2009 at 7:40 AM
Edited Jan 7, 2009 at 8:02 AM
You've right, this code <%= N2.Context.Current.SecurityManager.IsEditor(Page.User) %> return False.

What should I do to set my admin user to be Editor ?

Here my Né section in web.config:
    <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="2" />
        <engine>
            <assemblies>
                <add assembly="N2"/>
                <add assembly="N2.Edit"/>
                <add assembly="N2.Security"/>
                <add assembly="N2.Templates"/>
            </assemblies>
        </engine>
        <!-- Other flavours: SqlServer2005, SqlServer2000, MySql, SqLite -->
        <database connectionStringName="IPInterne" flavour="SqlServer2005"
      caching="true" cacheProviderClass="NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2" />
        <edit>
            <installer checkInstallationStatus="true" />
        </edit>
    </n2>

Edit : I found a post from you Libardo which said what to do.
So I update my web.config to include Administrator role to my admin user.
Now I can delete my page and see the Trash.

Thank you for help.
Jan 7, 2009 at 8:43 AM
Edited Jan 7, 2009 at 8:46 AM
Ok, now I have templates, Trash, I can delete.
Here my last 2 problems:

1 : In the html text editor, all editor's texts are strange, seem to be a resource string not converted into text
http://img132.imageshack.us/img132/2811/imageej1.png

2 : I see default template when I create a new page, How can I clear the template list to show only mine ?
Coordinator
Jan 7, 2009 at 7:05 PM
1: Possibly missing files. Try debugging with fiddler or firebug and look for 404s
2: The items displayed are classes deriving from N2.ContentItem compiled into a dll in /bin. Can you see if there is one remaining after the example project?
Jan 8, 2009 at 8:57 AM
Thank you for reply,

1: Just missing the french language pack of tinymce.
2 : after all, I'll keep them.

Thank you so much for help.