Best practices

Aliased custom 404 error page

This article describes how to create a custom 404 error page in Ektron CMS that will return a HTTP status code 404, use a friendly aliased URL and give content authors the ability to change the content within the page.

Create the template

Create a new ASPX Web Form template called Error404.aspx (or something similar).  Add a ContentBlock to the HTML source view and set the DynamicParameter property to “id”.

Add the template to the CMS in the Settings > Configuration > Template Configuration section in the Ektron workarea.

Now create a new content item in the CMS and assign it to the Error404.aspx template that you created earlier.  Assign an alias to the content item, something like “/page-not-found/”.

Test the page loads of by browsing directly to /page-not-found/.

HTTP status code 404

Now you need to tell the template to return the correct HTTP status code.  At present it is returning a status code of 200 which means “success”.  This will cause search engines to incorrectly believe that your 404 page is actually valid content – in other words, it will be indexed and returned in Google’s results!

To prevent this, go to the code-behind file (this is in C# of course):

    protected void Page_PreRender(object sender, EventArgs e)
    {
        Response.TrySkipIisCustomErrors = true;
        Response.Status = "404 Not Found";
        Response.StatusCode = 404; 
    }

What this does is set the HTTP status code to be 404.  This will indicate to search engines that they have found a missing page so they will not index it.

You can test your page by browsing to an invalid page and seeing if the custom error page is returned.

Enable custom error handling

The next step is to tell .Net that we want to use a custom error page.  This is done within the web.config file like so :

    <system.web>      
      <customErrors mode="On">
          <error statusCode="404" redirect="/page-not-found/" />
      </customErrors>
    <system.web>

This tells .Net to send any “404 page not found” errors to an alias called “/page-not-found/”.

Problems with invalid aliases

You may find that your custom 404 error page isn’t being activated if you try to browse to an invalid alias.  What I have found is that you instead get a nasty server 500 error instead:

Server Error in Application “###”

Internet Information Services 7.5

Error Summary

HTTP Error 500.0 – Internal Server Error

Internal Server Error

Detailed Error Information

Module ManagedPipelineHandler
Notification ExecuteRequestHandler
Handler ExtensionlessUrlHandler-Integrated-4.0
Error Code 0x800703e9
Requested URL http://###:80/wibble/
Physical Path C:\inetpub\wwwroot\###\wibble\
Logon Method Forms
Logon User ###

Eeek!  Where is my custom error page?!?!

I discovered that the problem was due to an incorrect setting in my web.config file.  I needed to remove a handler called “ExtensionlessUrlHandler-Integrated-4.0”, like so :

<system.webServer>
    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    </handers> 
</system.webServer>

(Please note I am using IIS 7)

By removing this HTTP handler it allows the request to be passed through to your custom 404 error page instead.

Create page base classes for Ektron templates

A quick time saver tip today… On my projects I like to create base classes from which items such as ASPX templates and ASCX user controls can inherit.  Base classes are Class Files that are placed into the App_Code folder.  This allows me to place commonly used code into one place that is then made available to all the templates and/or controls in the project.

For example, I have a C# class called “BasePage” which has a public property of “ContentId”.  This property holds the Ektron Content ID for the current paqe.  The Init event of the BasePage class will look for “id” or “ekfrm” or “pageid” on the querystring and use the value to populate the ContentId property.  This means that every ASPX template or ASCX control has a property called “ContentId” from which you can quickly get the content id.

Prior to this I found I was always writing code to get the content Id from the querystring (typically an Ektron template takes in a single content Id).

With these classes in place you will soon, over time, continue to add to them and ultimately this will save a little bit of time here and there.

Restricted folder names in Ektron

This one has caught out people a few times.  There are certain values which cannot be used as folder names within Ektron CMS.  They are in effect, protected names which are used by the CMS.  The problem is that it is still possible to use these values (i.e. there is no validation to stop you).

A list of the names I am aware of are here :

  • assets
  • privateAssets
  • workarea
  • uploadedfiles
  • uploadedimages

You will notice that each of these corresponds to a physical folder path in the root of a typical Ektron website.  This is where the problem comes in.  There are lots of code files (both .Net and Javascript) which reference relative folder paths using these values.  So, if you do happen to use these folder names you may get unexpected behaviour in the workarea and on your templates.

The one that usually catches people out is “assets” as this is a good name to give to a folder full of images or PDFs.  Now that you know this, please avoid using them!

If you have already done this, there is a work around here