A very basic content management system with multiple nested pages, comments and maybe a basic user management interface. Everything will be in ASP.Net MVC 2 in C#.
I’ve been meaning to do this for quite some time, but work and life kept getting in the way. I thought if I’m going to update for real, I’d at least post something useful.
It would be helpful to have some prior knowledge of MVC basics, but not an absolute prerequisite. Things will go a bit smoother if you’re already familiar with it and Visual Web Developer Express, which my IDE of choice for this one. I’m also going to be using Sql Server Express 2008 and I won’t be adding an MDF to the App_Data folder as most quick examples would show, but create a database in SQL Server using the Management Studio instead. I think it’s far simpler to deploy to a hosted service with this starting point.
Because I’m going to be posting a lot of code in this, there is the potential for mistakes in my train of thought or during formatting. Please point them out if you happen to come across them or if you have improvements. But please keep in mind that this is a SimpleCMS, so we’re not going to get all that fancy ;)
Let’s start with a ContentPage table in your database :
Note that only three fields are required and one is by default because it’s the primary key. This is because I thought of having the most flexible arrangement program-wise without being concerned too much about required fields in the table. And because I wanted the CMS to double as a forum at some point… More on that later.
Now we need a ContentComments table :
Now we need to add a parent-child relationship by adding a foreign key to the ContentComments table. In this case, the PageId of the ContentComments table is going to be the foreign key to the ContentPages table’s PageId primary.
Now back in your solution explorer, right click on the Models folder and Add > New Item. We’re going to add the Linq to SQL data classes here and call it SimpleCMS (you can call it any name of your choice, but just remember to keep it matching the project name for simplicity.
You’ll see the classes designer open with two sections exposed. You need to drag and drop the ContentPages and ContentComments tables into the class designer view.
Now that the database is set, we’re on our way to the fun part; the classes. But first, I’m going to break one of my own rules here and develop the template. This is because the default MVC theme is a bit too bare and, I believe, not very flexible even if you decide to hack it to pieces.