[Code at the bottom]
I had a client that needed a solution that could meet the following requirements:
- Must be able to add and remove page from their website
- Must be able to change the content of each of the pages
- Users of the website would need to be able to have a logon state
- The pages would need to show up in the navigation menu
- Must be able to do all of this without using a developer, re-engineering their site, installing a CMS system, or taking more than 4 hours of custom development time.
They also had the following merits:
- Those who would change the content were proficient in Html, CSS, and basic web technology
- All pages would have the same layout
- Existing website written in ASP.NET MVC
In the end, the content managers had access to a "PartialPages" folder that set under the main site on the webserver. The managers could add, remove, and edit these files. They could also update the SiteMap.xml file for these pages, allowing for the navigation menu to update based on their changes.
CODE:
AreaRegistration - Routing:
context.MapRoute(
"FiveMinuteCMS_Default", // Route name
"FiveMinuteCMS/{id}", // URL with parameters
new { controller = "FiveMinuteCMS", action = "Pages", id = UrlParameter.Optional }
);
Controller:
public ActionResult Pages(string id)
{
PagesModel model = new PagesModel();
model.Page = System.IO.File.ReadAllText(HttpContext.Server.MapPath
("~/PartialPages/" + id + ".html"));
return View(model);
}
Model:
public class PagesModel
{
public string Page { get; set; }
}
Pages View:
@model MySampleProject.Areas.FiveMinuteCMS.Models.PagesModel
@{
// Any layout will do
Layout = "~/Areas/FiveMinuteCMS/Views/Shared/_Content.cshtml";
}
@Html.Raw(Model.Page)
Sample Partial Page file:
<div id=”PartialContentBody”>
<span>Hello World</span>
</div>
For the site map and navigation, I used Maarten Balliauw's MvcSiteMapProvider