Getting Started with Content Management System Part I
In simple words what we are building now is a Website with multiple pages where all the content comes from a Database.
As one of the feature of a Strong CMS is it uses a database to store page content,meta data, and other information assets that might be needed by the system to display the page.
In this article I will not be discussing about the Member Management , Permissions , Administrations etc.
Getting Started with Content Management System Part I
In today's internet market the Websites we develop have to be Dynamic and Easy to change.
Although there are many open source CMS Systems that are available to use it as a Developer it is necessary for us to understand the working of a CMS.
In simple words what we are building now is a Website with multiple pages where all the content comes from a Database.
As one of the feature of a Strong CMS is it uses a database to store page content,meta data, and other information assets that might be needed by the system to display the page.
In this article I will not be discussing about the Member Management , Permissions , Administrations etc.
In this article I will be discussing only about the basics of a Content Management System and how to build a simple Dynamic Pages Website as the Sample.
Functions included in this article will be :1) Adding and Updating Pages
2) Retrieving the content for the pages from the database.
3) Working with simple URL Rewrite using Global.asax.
1) Adding and Updating Pages
This is the most easy part. For developers who already know how to insert records in SQL Database.
In this example I have used a table with this structure.
Table Name : Pages
Pid : Int
PageName : Varchar(255)
PageTitle : Varchar(255)
PageDescription :Varchar(255)
PageContent : Text
PageKeyword : varchar(255)
This table will be used for retrieving Page Details.
In the sample attached you will find the Page "ManagePages.aspx" which has the logic for the Adding and Updating the records in the Pages Table.2) Retrieving the content for the pages from the database.
For retrieving records from the table I have made a class
Public Class PageDetails
Public Property PageTitle As String
Public Property PageDescription As String
Public Property PageKeyword As String
Public Property PageName As String
Public Property PageContent As String
End Class
and a function which will retrieve the Page Details.
Public Function GetPageDetails(ByVal pagename As String) As PageDetails
Dim dt As New DataTable
Dim dtap As New SqlDataAdapter("select * from pages where pagename='" & pagename.Replace("-", " ") & "'", sqlcon)
dtap.Fill(dt)
Dim pagedet As New PageDetails
pagedet.PageName = dt.Rows(0)(1)
pagedet.PageTitle = dt.Rows(0)(2)
pagedet.PageDescription = dt.Rows(0)(3)
pagedet.PageContent = dt.Rows(0)(4)
pagedet.PageKeyword = dt.Rows(0)(5)
Return pagedet
End Function
The pages will be displayed on a Page called Pages.aspx which you will find in the Sample project. In the Pages.aspx.vb I have written the following code :
Dim comfunc As New CommonFunctions
Dim pageDet As New PageDetails
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("id") <> Nothing Then
Dim id As String = Request.QueryString("id")
pageDet = comfunc.GetPageDetails(id)
Me.Title = pageDet.PageTitle
Me.MetaDescription = pageDet.PageDescription
Me.MetaKeywords = pageDet.PageKeyword
pagecontent.InnerHtml = pageDet.PageContent
End If
End Sub
What happens with this Code is that if the user types http://localhost/samplecms/pages.aspx?id=about-us
The function will try to get the details for the page about-us
This page will have a query string as Page Name and will return the record which has the same page name.(Remember to use this example in a live project you will have to give some sort of validations to avoid duplicate Page Names)3)Working with simple URL Rewrite using Global.asax.
These urls which you see with querry strings like "pages.aspx?id=about-us" is not Search engine friendly to make it easy for Search Engines to crawl we need to use some sort of URL rewriting you can google for different much better ways of URL Rewriting but the one I find the most easiest is handling the Url in the BeginRequest Event of the Global.asax file.
So in the BeginRequest event of the Global.asax file I have written the following code.
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
If Path.GetFileName(Request.Path).ToLower().StartsWith("page-") Then
HttpContext.Current.RewritePath("pages.aspx?id=" & Path.GetFileNameWithoutExtension(Request.Path).tolower.Replace("page-", ""))
End If
End Sub
The code checks for page- in the url and when it finds it rewrites the path to pages.aspx?id= sending the value of the query string.
In this example if the user hits the page "page-About-us.aspx"
there is no such page in the project but the rewrite path method will redirect it to pages.aspx?id=about-us.
Then what happens is already discussed in the 2nd Point of this article.
Hope this article has made things clear about getting started with developing a CMS system.
Great article, truely valuable.
Can you give some C# codes?