Server.Transfer throws Error executing child reque

2019-02-11 16:15发布

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?

7条回答
Anthone
2楼-- · 2019-02-11 16:19

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.

查看更多
迷人小祖宗
3楼-- · 2019-02-11 16:21

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

查看更多
4楼-- · 2019-02-11 16:22

I found an alternative to Server.Transfer()

I used

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

This solved the issue.

查看更多
Luminary・发光体
5楼-- · 2019-02-11 16:22

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.

查看更多
Deceive 欺骗
6楼-- · 2019-02-11 16:28

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

查看更多
forever°为你锁心
7楼-- · 2019-02-11 16:34

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.

查看更多
登录 后发表回答