Server.Transfer throws Error executing child request. How to resolve?
Asked Answered
G

8

33

I have a HttpModule in C# 2.0 which handles exceptions thrown. Whenever the exception is thrown, an error page (aspx) with some querystring will be called. It is done through Server.Transfer().

But when the control tries to execute Server.Transfer(), the following exception is thrown:

Error executing child request for [pagename].aspx.

Whereas Request.Redirect() works fine.

I tried setting EnableViewStateMac="false" in Page directive of the page to which request is transferred. Still problem persists.

Here is the code I tried:

string errorPage = "errorpage.aspx?id=" + someErrorId
HttpContext.Current.Server.Transfer(errorPage,true);

Any idea how this can be resolved?

Gazo answered 2/3, 2010 at 8:21 Comment(0)
G
19

I found an alternative to Server.Transfer()

I used

 HttpContext.Current.RewritePath("somefile.aspx");

This solved the issue.

Gazo answered 2/3, 2010 at 9:48 Comment(3)
How did you use it? The exception Error executing child request disappeared, but I get an empty page.Relegate
You have to do it in a IHttpModule. See msdn.microsoft.com/en-us/library/ms972974.aspx (unfortunately the article is way overlong, the actual concept is pretty simple).Implead
If you have Server.Transfer available to you, you can probably just call Context.RewritePath, rather than needing to call HttpContext.Current to get an HttpContext object :)Alga
A
16

I was hooking into the request pipeline in the OnPreRequestHandlerExecute event, and found I couldn't use Server.Transfer because it threw the same error as yours about executing a child request.

Using HttpContext.Current.RewritePath didn't work because it seemed to be ignored and I wasn't redirected anywhere.

If you're using IIS 7 and up, you can use Server.TransferRequest instead which did the trick for me.

The differences between the two methods are covered in this answer: TransferRequest vs Transfer in ASP.Net

Acquisition answered 17/2, 2015 at 16:21 Comment(1)
Worked for me. Other answers didn't.Freezedry
I
8

My fix was different:

An online query produced this Microsoft Knowledge Base article which stated the resolution would be to use Response.Redirect instead of Server.Transfer.

I changed the command and got a more accurate "404 Error Message" instead of the cryptic "Error executing child request" message.

That led me to inspect the redirect string and I noticed my path was off.

I fixed the Transfer String from "ErrorPage.aspx" to "../ErrorPage.aspx" (notice the path change) and Server.Transfer worked just fine.

Intermittent answered 21/2, 2014 at 17:0 Comment(2)
I found that I needed to change this to "~/ErrorPage.aspx", or else it would stop working when I moved from a sub-directory up to the application root (see my answer)Transilluminate
We were using Server.Execute and got the message. Thanks! It was also a 404 Error.Abdul
D
4

If you happen to see this exception occur in the VS.NET IDE during debug, go ahead at least once and press F5 to continue debugging. In my case, the actual page did render with the ASP.NET exception that was really causing the issue. In my case I had an incorrectly formatted asp:ChangePassword control that was actually causing the "Error executing child request" exception.

Dandle answered 18/10, 2012 at 14:54 Comment(1)
I believe this is also exposed in the InnerException property of the exception. In my case it was a missing user control.Diplegia
P
2

I changed Server.Transfer for Server.TransferRequest, then I've got the real exception behind. It was a problem with a reference to an incorrect ReportViewer version.

Phillips answered 12/7, 2019 at 16:4 Comment(0)
L
0

Server.Transfer("mywebpage.aspx") seems to work only when a session already exists.

If there is no Session started it throws this error so you must use Response.Redirect or another method.

Leventis answered 28/5, 2014 at 11:58 Comment(0)
T
0

I had this same problem, and discovered it was to do with the relative paths of where the Server.Transfer is running and where you are redirecting to. In my case, this was executing in a sub-directory, so @ray's answer (adding ../ to the start of the URL) worked. However, when I then executed in the directory above, the same thing happened. Therefore, I changed to using a rooted path:

Server.Transfer("~/errorpage.aspx")
Transilluminate answered 27/9, 2016 at 17:49 Comment(0)
A
-1

It isn't a bug, it is by design. See http://support.microsoft.com/kb/320439

Affix answered 2/3, 2010 at 10:1 Comment(4)
Yes I had seen that. But I had a requirement not change the URL on the address bar.Gazo
He isn't redirecting to ASPFletafletch
It happens whereever you redirect.Affix
It is only "by design" for a specific use case in the linked articleDoggerel

© 2022 - 2024 — McMap. All rights reserved.