Custom Areas

Coordinator
Apr 22, 2009 at 6:55 PM
Our intention when we built the CMS was to act as a framework to sit beside any bespoke developments you'd want to do in MVC.

Well we never really thought as far as integrating the two developments so templating was never really considered.

The first site we built using it required exactly that so this is how I went about merging the two:

  1.  In your global.asax add the following code (replace where appropriate)

    routes.CreateArea("MyArea", "OpenQuarters.WebQuarters.Areas.MyArea.Controllers",
                    routes.MapRoute(null, "MyArea/{controller}/{action}/{id}", new { controller = "MyDefaultController", action = "Index" })
                );

  2. Then create your controller(s) in your own area space. I used /Areas/MyArea/Controller for example.
  3. Inherit your controller(s) from the base CMS controller: OpenQuarters.WebQuarters.Areas.Site.Controllers.HomeController
  4. Create a single proxy page in your hierarchy (and in database directly set a reference value in my case MyAreaPage)
  5. Define a global CMSPage variable (for ease) and a constructor for your controller and have it populate the global CMSPage with your proxy page

            CMSPage cmsPage = null;

            public MyDefaultController()
            {
                cmsPage = CMSPage.LoadByReference("MyAreaPage");
                Index(cmsPage.PageId.ToString());
            }

  6. Next you need to create a new template for the proxy page (don't forget to assign it) and in place of your content area (or where ever you want your custom view stuff) add the following:

            <% Html.RenderPartial("~/Areas/MyArea/Views/" + ViewData["View"] + ".aspx", ViewData["MyModel"]); %>
  7. Now the only untidy part is that you cannot used strongly typed ViewData (please suggest how to get round this) due to the templates requiring a CMSPageVersion instance
  8. I also added this line to redirect anyone hitting the proxy page by mistake

            <% if (ViewData["View"] == null) Response.Redirect("/MyArea/MyDefaultController"); %>
  9. When coding your controller you need to return the following (or similar)

                ViewData["View"] = "MyArea/Index";
                ViewData["MyiewData"] = viewData;
                return View("~/Areas/Site/Views/Home/Index.aspx", cmsPage.CurrentVersion);

  10. The last thing to do is code your views using the old skool ViewData syntax

                 ViewData["MyViewData"]