Create error pages in Composite C1

Custom error pages in IIS look very great, but in case of a CMS, they seem to be too static most of the time. Here is a short explanation how you can create correct error pages with Composite C1 CMS.

23 March 2014
Christoph Keller Christoph Keller

First of all, you have to configure the correct error pages in your web.config.

For this you're first have to remove already available error pages for the statusCode, to make sure we hit that configuration for sure. After that, we need to set the new error pages to a URL which will be handled by Composite C1. In my example, I created two pages with the URL /Errors/PageNotFound and /Errors/ServerError.

Hint: Set the menu-title of these pages to an empty string, to avoid that the error pages show up in your navigation.

To be sure that the URL does not change in the users browser if an error occures, you should set these error entries to responseMode="ExecuteURL". With this attribute, the call to the C1 Pages will be rewritten on the server and not redirected by the user's browser.

The web.config configuration should now look like this.

<configuration>
	<system.webServer>
		<!-- OTHER STUFF ABOVE, DEPENDING ON CONFIGURATION -->
		<httpErrors errorMode="Custom">
			<remove statusCode="404" />
			<remove statusCode="500" />
			<error statusCode="404" responseMode="ExecuteURL" path="/Errors/PageNotFound" />
			<error statusCode="500" responseMode="ExecuteURL" path="/Errors/ServerError" />
		</httpErrors>
		<!-- OTHER STUFF BELOW, DEPENDING ON CONFIGURATION -->
	</system.webServer>
</configuration>

Now the redirect works like a charm. But there is still a pending issue with that. The server-rewrite removes the original HTTP Status Code (404 or 500 in our case), and that's bad for search engines, as they cannot decide wether the document was delivered correctly or not. For that, I created a new Razor function which I just add to the page and configure the desired HTTP Status Code.

@inherits RazorFunction

@functions {
    public override string FunctionDescription
    {
        get  { return "Sets the response status code to the desired value."; }
    }
     
    [FunctionParameter(Label = "The desired response code to add")]
    public int DesiredResponseCode { get; set; }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
    </head>
    <body>
		@{
			Response.StatusCode = DesiredResponseCode;
		}
    </body>
</html>

And voilá, the redirect works and the page delivers the correct HTTP Status Code. :)

That's it, hope it helps anyone out there.

If you have any questions, suggestions or comments, don't hesitate to contact me.


comments powered by Disqus