Gatis Bergšpics
Sep 26, 2011
  22564
(2 votes)

Server.Transfer issue, "Error executing child request for /404."

The goal was to create EPi server operator(administrator) modifiable 404 error page. To do this I created new simple page type called ErrorPageType(with very simple aspx file):

[PageType("633fd95b-d6bd-41e5-bc6b-19bc0247f599",
        Name =
"Error page"
,
        Filename =
"/Templates/Pages/ErrorPage.aspx"
)]
   
public class ErrorPageType : TypedPageData

    {
 
        [
PageTypeProperty(EditCaption = "Error page title",
            SortOrder = 0,
            UniqueValuePerLanguage =
true
,
            Required =
false
,
            Searchable =
false
)]
       
public virtual string PageTitle { get; set
; }
 
        [
PageTypeProperty(EditCaption = "Error page body"
,
            SortOrder = 1,
            UniqueValuePerLanguage =
true
,
            Required =
false
,
            Searchable =
false
)]
       
public virtual string MainBody { get; set
; }
 
    }
<!DOCTYPE …>

<html >

<head id="Head1" runat="server">
   
<title><%= CurrentPage.PageTitle %></title>
</head>
<body>
   
<form id="form1" runat="server">
   
<div>
    
<%= CurrentPage.MainBody %>
   
</div>
   
</form>
</body>
</html>

Whole reason behind using Server.Trasfer(instead of Response.Redirect) is following - SEO(search bots most likely wont follow resource moved to that page hints, or wont display them properly in search results. Web browsers however will and there would not be problems). So to achieve that I created new page in EPi server and called it 404 from previously created page type. I tough that this is it - straight forward, but it turned out it is not so, far from it.

My first approach was:

Server.Transfer("/404");

This resulted in following exception: "Error executing child request for /404." of type HttpException that led me to following blog posts:

http://labs.episerver.com/en/Blogs/Svante-Seleborg/Dates/2008/10/When-a-404-Not-Found-should-be-a-404-Not-Found/

and

http://world.episerver.com/Blogs/Dan-Matthews/Dates/2010/3/A-few-tips-on-UI-Plugins-in-CMS6/

So I tried this(no need to disable friendly URLs, because this still should work):

Server.Transfer("/Templates/Pages/ErrorPage.aspx?id=2421&epslanguage=en");

But it did not change anything, so I made constructor for ErrorPage class similar to one Svante Seleborg suggests to use:

public partial class MyPage : SimplePage
{
  public MyPage() : SimplePage(0, HttpContext.Current.Items["InErrorHandler"] == null ? 0 : PageExtensions.SaveCurrentPage.OptionFlag)
  {
  }
}

This led me to the same exact exception, then in Dan Matthew entry I read something similar with optionFlag only this time he suggested to disable different optionFlag that did not help me either.

Next idea was to just pass integer 65535, I thought that this will disable all options and it did, and Server.Transfer and Server.Execute started to work.

At this point I tough that I even do not know what I'm disabling so I went out to search what options are there,did that following way:

Opened up http://sdk.episerver.com/library/cms6.1/index.aspx and searched for OptionFlag, and I found 14 different result and scratched class names out of them, so at first I tried passing all of then, then removing one by one and result was following:

protected ErrorPage()
          
//  : base(0, 65535) //this works as expected

            :
base(0, 
           
HttpContext.Current.Items["InErrorHandler"] == null
? 0 :
            0
          
// | AntiForgeryValidation.OptionFlag

          
// | CultureSupport.OptionFlag
          
// | LoadCurrentPage.OptionFlag
          
// | CustomPageLink.OptionFlag
          
// | ThemeUtility.OptionFlag
          
// | SiteRedirect.OptionFlag
            |
ContextMenu.OptionFlag //this is the one that needs to be disabled
          
// | SaveCurrentPage.OptionFlag
          
// | ServerTransferBugfix.OptionFlag
          
// | SetupContentRedirect.OptionFlag
          
// | PageVisited.OptionFlag
          
// | PageStatistics.OptionFlag
          
// | PageNotFoundRedirect.OptionFlag
          
// | PageTranslation.OptionFlag
            )
        {
        }

At the end of the day, my code looks something like that:

public void TransferToHttpErrorPage(int httpErrorCode)
{
    Response.Clear();
    Response.StatusCode = 404;
 
   
try

    {
       
HttpContext.Current.Items["InErrorHandler"] = true;
                    
       
var factory = new DataFactory
();
 
       
// get error page reference from start page

       
var startPage = factory.GetPage(PageReference.StartPage);
       
if (startPage != null
)
        {
           
var reference = startPage[string.Format("Error{0}PageReference"
, httpErrorCode)];
           
if (reference != null && reference is PageReference
)
            {
               
var page = factory.GetPage((PageReference
)reference);
                Server.Transfer(page.LinkURL);
               
return
;
            }
        }
                
       
// startPage not found or Error{0}PageReference not found

       
// get error page reference by friendly URL
 
       
object pageReference;
       
if (EPiServer.Global.UrlRewriteProvider.ConvertToInternal(new UrlBuilder(string.Format("/{0}", httpErrorCode)), out
pageReference))
        {
           
var page = factory.GetPage((PageReference
) pageReference);
            Server.Transfer(page.LinkURL);
           
return
;
        }
 
       
// if get this far use static error page

        Server.Transfer(
string.Format("/{0}.htm", httpErrorCode));
    }
   
catch (Exception
ex)
    {
        Response.Write((
string.Format("Http error {0} has occurred"
, httpErrorCode)));
        Response.End();
    }
}

I'm new to EPi Server so I have a question to more experienced EPiServer developers: is this a bug or feature, can someone explain why in order for Server.Transfer(or Server.Execute) to work I need to disable ContextMenu option in page?

I used latest EPi Server CMS 6 R2

Sep 26, 2011

Comments

Muhammad Kashif
Muhammad Kashif Sep 27, 2011 03:21 PM

Hi
I've recently implemented the multilingual 404 pages for one of my client site - I've pretty much used the same approach (with some additional multi-lingual handling) .

But I don't have to do any additional code to handle server.transfer . I think there was a bug in earlier version of EPiServer which now has been resolved through PageExtensions plugin . You can enable / disable this plugin , under plugin manager .

Muhammad

Gatis Bergšpics
Gatis Bergšpics Sep 28, 2011 12:12 PM

Muhammad, would not that disable this feature for entire website, it would, in this case only error page is affected.

In fact it seems that this problem is repeatable, when user is logged in in EPi server as administrator, and aspx serving error page is missing HTML, but only contains plain output, in this case - EPi server tries to add this context menu and it fails if no proper html exists on page.

if one uses IIS7 or newer the Server.TransferRequest can be used instead of Server.Transfer or Server.Execute can be used and context menu will work(needs not to be disabled anymore) if user logged in as an administrator!

Please login to comment.
Latest blogs
Optimizely Opal: How to Build Effective Workflow Agents

If you're building workflow agents in Optimizely Opal, this post covers how specialized agents pass context to each other, why keeping agents small...

Andre | May 20, 2026

ReviewPR: An Azure Function That Reviews Your Azure DevOps Pull Requests With Claude

A while back I wrote about an  Azure Function App for PDF creation that we use to offload PDF rendering from our Optimizely DXP site. That same...

KennyG | May 19, 2026

Accelerating Optimizely CMS and Commerce upgrades with agentic AI (Part 2 of 2)

The Real Transformation in Optimizely CMS 13: Why the Upgrade Itself Is the Easy Part. A field-tested playbook for enterprise teams moving from...

Hung Le Hoang | May 18, 2026

Is the most powerful AI model really the best value?

Artificial Intelligence is already becoming part of everyday software development. Developers now use AI tools to generate code, write documentatio...

K Khan | May 16, 2026

Optimizely London Dev Meetup 2026

Well, everyone, it's that time of the year again, and we have another London Developer meet up coming for this summer. The date is set for the 2nd ...

Scott Reed | May 15, 2026

Semantic Search - Deep Dive

Deep dive into semantic search with Optimizely Graph

Michał Mitas | May 14, 2026 |