Views: 16106
Number of votes: 2
Average rating:

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

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

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.