I've built a pretty simple content manager for a website that I have. For every page, the user has control over what content is displayed by using an Admin tool.
Now, my client wants the ability to add entire pages. Right now, I have to create a new page, and then they can go in and edit the content for that page. But they don't want to do that anymore, they'd like to create their own without having to com开发者_运维问答e to me. They'd like the name of the page to be consistent as well - so if they create an "About" page, it would be mysite.com/about instead of mysite.com/dynamicpage/8.
I need to be able to do this without using a third party CMS, considering that there already a robust Admin backend. Is there any tutorial about how to do this?
I'm using ASP.NET MVC 2, .NET 4.0.
A simple solution here (and you should probably customize it a bit):
Simply create a route that contains a variable (say pageName or something) on a ContentController or something. AKA (mysite.com/content/{pageName}). Your action method takes the page name, and then loads up the HTML via the database. It then simply returns that HTML as a Content Response
return new ContentResult
{
Content = htmlFromDatabase,
ContentType = "text/html" // Change if you want depending on DB values
};
Then you just need one admin panel to update the DB entries with content and the client can add / remove / edit entries that just exist in the DB.
While I haven't tried it you can have a look here http://haacked.com/archive/2009/04/22/scripted-db-views.aspx
And it has already been asked and answered in SO Create Dynamic pages in asp.net mvc
If you can move to MVC3, you can use Razor's ability to execute scripts (pages) from a string.
I'm using this open-source project to do something very similar.
Then, all you need to add is a page in your admin area to edit the virtual pages, and stuff the result into a database.
Your controller will examine the requested route and retrieve the correct page record from your database, use Razor to execute the page (and get the result as a string), and send the result back to the client.
This will let you easily include dynamic content within the page (the script can, say reference partial views that return the dynamic items). If all they need to do is edit HTML, it's probably overkill.
精彩评论