views:

187

answers:

1

In a content management system you can usually create pages on the fly eg

www.website.com.au/home.aspx

www.website.com.au/projects.aspx

www.website.com.au/contact-us.aspx

In a previous CMS that we wrote we physically created these files on disk when the user selected to create a new page in his site. We copied the new file from a base template page, renamed the file and renamed the class in the code behind eg

template_page.aspx and template_page.aspx.cs turned into

projects.aspx and projects.aspx.cs

This was all done via our CMS application. No files needed to be manually created by a user.

How would this approach work using MVC?

Eg www.website.com.au/home/

www.website.com.au/projects/

www.website.com.au/contact-us/

Presumably we would need to dynamically create controllers and views on the fly?

This seems even messier than the old approach but I suppose its feasible.

Can anyone think of a smarter way to do it?

A: 

You should be able to use one controller and a couple views (display, create, edit) with some routing work. I did a super simple implementation for a personal project that went like this. I put this route near the top of my routing list and used the constraint to determine if it should be considered as a static page from my rules. My implementation didn't have any sort of hierarchy, i.e. pages/About-us/contact - only /contact.

route:
routes.MapRoute("StaticContent", "{title}",
  new { controller = "Page", action = "Details"},
  new { title = new InvalidTitleContstraint()});


controller:
public class PageController : Controller
{
    // Details checks if it can find a matching title in the DB
    // redirects to Create if no match 
    public ActionResult Details(string title)
    // GET
    public ActionResult Create()
    // POST
    public ActionResult Create(Page page)
}
Ben Robbins