MVC Project issues

Topics: Developer Forum
Jul 14, 2008 at 2:41 PM
Hi there

I'm trying to get the MVC project to compile and run but there seems to be various problems in both the release version and the version from the SVN trunk.

Currently with the version from Trunk it is missing references to the MVC dlls, as well as N" and N2.Extensions. I have taken the MVC dlls from the lib folder, and the other two directly from their respective projects, which has enabled me to successfully compile the MVC test project. Unfortunately at runtime I get the following error from Windsor:

"Required configuration property startPageID not found"

I think that perhaps I'm not installing everything properly - the Prepare_Dependencies-vs2008.bat fails for me.

Any advice on how to get a working MVC project up and running would be most appreciated - I would much ratehr use MVC than web forms!

Thanks very much
Jul 14, 2008 at 3:43 PM
Hello, some quick and dirty hacks to make Mvc sample running (at the very least ;-):

1. Drop any N2.mdf into ~/App_Data/. (In a recent release you can grab one from Deploy\Source\examples\MediumTrust\wwwroot\App_Data after you've finished with Deploy_Everything-vs2008.bat. Btw, the order you should run batches is this: 1) Build_Everything-vs2008.bat 2) Deploy_Everything-vs2008.bat -- don't worry about eventual errors being reported -- they're most probably related to unit testing stuff)

2. web.config is somewhat out of sync with a recent N2 engine, so you should update it manually:
2.1 Remove all references to <castle/> (E.g.: from <configuration><configSections/></configuration> and elsewhere..)
2.2 Add a new-style declarations as in the following lines:

<configSections>
...
<sectionGroup name="n2"
                     type="N2.Configuration.SectionGroup, N2">
            <section name="web"
                     type="N2.Configuration.WebSection, N2"
                     requirePermission="false" />
            <section name="host"
                     type="N2.Configuration.HostSection, N2"
                     requirePermission="false" />
            <section name="engine"
                     type="N2.Configuration.EngineSection, N2"
                     requirePermission="false" />
            <section name="database"
                     type="N2.Configuration.DatabaseSection, N2"
                     requirePermission="false" />
            <section name="edit"
                     type="N2.Configuration.EditSection, N2"
                     requirePermission="false" />
            <section name="globalization"
                     type="N2.Configuration.GlobalizationSection, N2"
                     requirePermission="false" />
        </sectionGroup>
</configSections>

2.3 Add the following N2 config section somewhere inside <configuration/> section:

<n2 xmlns="http://n2cms.com/schemas/configuration/v2">
        <!-- 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="1"
              multipleSites="false"/>
        <engine extension="/">
            <assemblies>
                <!-- These are only needed for medium trust
        <add assembly="N2"/>
                <add assembly="N2.Security"/>
                <add assembly="N2.Edit"/>
                <add assembly="N2.Templates"/>
                <add assembly="N2.Templates.UI"/>
                <add assembly="N2.Templates.Survey"/>
        -->
            </assemblies>
            <errors action="Email"
                    mailTo="cristianlibardo@hotmail.com"
                    mailFrom="noreply@n2cms.com" />
        </engine>
        <!-- Other flavours: SqlServer2005, SqlServer2000, MySql, SqLite, SqlCe, Firebird -->
        <database connectionStringName="N2CMS"
                  flavour="SqlServer2005" />
        <edit>
            <installer checkInstallationStatus="false" />
        </edit>
        <globalization enabled="true" />
    </n2>

2.4 Mvc sample seems to use ASP.Net Mvc Preview 2. To make it work you need both dedicated http module, as well as a special http handler for routing. For some reason the latter is missing from web.config. Here's how it should be declared inside <httpHandlers/> section:

<add verb="*"
                 path="UrlRouting.axd"
                 type="System.Web.Routing.UrlRoutingHandler, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

(I expect question where to get System.Web.Routing.dll, but hope you'll find one ;-)

From now on chances are high the application will compile and run. New problem you'll see is the Yellow Screen Of Death with the something like this: 'Object with id: 1 was not of the specified sublcass: N2.ContentItem (Discriminator was: 'StartPage0') '. That's NHibernate compalining that it cannot resolve records in the dabase to any .Net types visible to it in all assemblies within your App Domain. So now it's time to some creative moves:

3. Look into ~/Models folder, examine non-abstract classes there: when NHibernate retrieves records from n2Item table, it looks into Type field and tries to find a corresponding .Net class either by it's name, -OR- if class is decorated with [Definition] attribute, then by attribute's second parameters. E. g. there's ContentPage class, decorated with [Definition("Content Page", "Test")] -- then you'll need to put value 'Test' into n2Item.Type column.

Please tell how it will go,
Good luck !
Jul 14, 2008 at 6:14 PM
Hi

Thanks for your help - I've got the project up and running now. I had to add a load of classes to the models directory (one for each distionct 'type' field value in n2Item in the db as outlined above). At the moment they all inherit from "AbstractPage" which I am fairly confident is wrong but I shall investigate further tomorrow in an effort to get everything cleared up.