Many thanks to Awe for the solution! I have implemented this code in a (error404.ascx) ASP.NET User Control as follows:
<%@ Control Language="C#"%>
With the following masterpage:
<%@ Master Language="C#" AutoEventWireup="true" Inherits="MyMaster" %>
Site title here
...