Entries Tagged as 'dnn.dotnetnuke'

Changing the DotNetNuke Page Title and Using dnn page titles

In a typical .Net web application you can change the title of a page on the fly by accessing the Page object’s Title property.

protected void Page_Load(System.Object sender,System.EventArgs e)
{
Page.Title = “My Custom Page Title”;
}

But from within your DotNetNuke module code you do it in a slightly different way. You first type-cast the Page object into a DotNetNuke.Framework.CDefault variable. Then you read and write the Title member on the CDefault object:

protected void Page_Load(System.Object sender,System.EventArgs e)
{
DotNetNuke.Framework.CDefault pageVar= (DotNetNuke.Framework.CDefault)Page;
cd.Title.Title = “My Custom Page Title”;//Set page title

titleDiv.innerHTML=”<b>”+cd.Title.Title.toString()+”</b>”;//Display Title of page In a Div at Page
}

If you compare Page.Title to CDefault.Title you will notice that Page.Title is a public property and CDefault.Title is a public member and you can use this for getting and setting page titles.

Let’s take a look under the hood of the framework. Inside the CDefault source you will find this code:

Public Class CDefault

Inherits DotNetNuke.Framework.PageBase
Public Comment As String = “”
Public Description As String = “”
Public KeyWords As String = “”
Public Copyright As String = “”
Public Generator As String = “”
Public Author As String = “”
Public Shadows Title As String = “”

You will notice that CDefault inherits from DotNetNuke.Framework.PageBase. Let’s take one step further and look at what the PageBase code looks like:

Public MustInherit Class PageBase Inherits System.Web.UI.Page

You can see that PageBase inherits from the System.Web.UI.Page class. What is going on is that CDefault is a descendant of PageBase, and inherently a descendant of the Page class as well. Thus it has all the members and method of those two classes.

However, CDefault defines its own public string called Title which “shadows” the Page.Title property. This essentially hides the Page.Title from Visual Studio’s IntelliSense when you’re viewing the properties and methods of a CDefault object.

Public Shadows Title As String = “”

So when you set CDefault.Title you are actually setting a different variable than Page.Title.

The nice thing about having the CDefault object around is that it exposes several other parts of the page for you to work with besides the title.

Public Description As String = “”

Public KeyWords As String = “”

Public Copyright As String = “”

Public Generator As String = “”

Public Author As String = “”

These members allow you to set the values of meta-data in your page header.

cd.Description = “My Custom Description”;

cd.KeyWords = “Coffee, Mojo, Hat”;

cd.Copyright = “My Company”;
cd.Generator = “Foo Coder”;
cd.Author = “Custom Author Name”;

Note that in the code above, cd is a CDefault object.

Knowing how to set these values can be important for those of you concerned with search engine optimization.
Working With The DotNetNuke Page URL

As you probably already understand, the typical DotNetNuke installation really only has one page, Default.aspx. The Default.aspx page receives the tab id (the id of the page it needs to display) via the query string using URL Rewrite. Inside the SiteURLs.config file in the root folder of your installation there exists the following rule which tells the URL Rewritting Http Module to convert the search engine friendly URL into a URL that passes the tab id to the default.aspx page via the query string:

<RewriterRule>
<LookFor>[^?]*/TabId/(\d+)(.*)</LookFor>

<SendTo>~/Default.aspx?TabId=$1</SendTo>

</RewriterRule>

This is a powerful tool that you can customize in order to make your module more functional.

Just as the DotNetNuke site uses the URL rewriter to reduce the page files down to just one, you can use the URL rewriter to reduce the module instance quantity down to just one. Let me explain.

Let’s say that you have a database table containing 10,000 records, each of which you want to provide individual pages for. Rather than generating 10,000 tabs / pages inside your DotNetNuke installation, you can create a URL rewrite rule like this:

<RewriterRule>

<LookFor>.*/Products/(.+)/(.+)\.aspx</LookFor>

<SendTo>~/Default.aspx?TabId=54&amp;brand=$1&amp;item=$2&amp;mode=view</SendTo>

</RewriterRule>

In this rule you tell the URL rewriter to look for a regular expression that matches a request that looks like http://www.yourdomain.com/Products/SomeBrand/ProductName.aspx, where SomeBrand can be anything and ProductName can be anything. Those two “anythings” will be the key to one of the records you wish to display from your database table. The rule rewrites the URL to point to a specific tab (tab 54) on which there exists an instance of your module. This allows you to have a unique URL for each of the 10,000 products in your database, but only one tab within DotNetNuke for displaying them.

Now your module can display whatever it pleases based on the friendly URL that was originally requested! Have your module fetch the brand and item parameters through the query string:

if (Request.QueryString["brand"] != null && Request.QueryString["item"] != null)
{
string path = Request.QueryString["brand"]+ “/” + Request.QueryString["item"];
// now use the path string as a
// key to a record in your database
}

Of course you need to take care not to pass on any old query string data on to your DAL. I usually run the values I get from the query string through some code like this which removes spaces and special characters:

// remove non [A-Za-z0-9] with spaces
myString = Regex.Replace(myString, “[^A-Za-z0-9]“, ” “);
// remove spaces
myString = myString.Replace(” “, ” “).Trim();

For the sake of simplicity you might consider passing the numeric id / key of the item in your database rather than the brand and product name text like the example above. By doing so you can keep your data redundancy low inside your database, and eliminate the chance of two items having the same path in the URL. On the other hand the URL containing only text might be considered more optimized for search engines. This is a trade-off you will have to consider on your own.

Final, here is one note regarding URLs for the Asp.Net beginners reading this. You will probably at some point want to advance the browser to one of the product URLs manually. This can be done using the Response.Redirect() method.

Response.Redirect(”/Products/”+ path + “.aspx”);