Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.Transfer in Global.asax

I have a custom error handler in the global.asax's Application_Error method, when an error occurs I use the following code to transfer user to the error page:

Server.Transfer("/Error/");

However without specifying the actual page name the code above breaks with "Error executing child request for /Error/" exception.

So if I use Server.Transfer("/Error/Default.aspx") it works fine with no problems.

Also using Response.Redirect("/Error/") works fine too, however we want to keep using Server.Transfer to retain URL displayed in the address bar when the error page is displayed so users can simply refresh the page to retry accessing the original offending URL.

Would be grateful if anyone can comment on how to get the Server.Transfer method working without specifying the actual aspx page name.

Many thanks.

like image 559
Maya Avatar asked Dec 29 '10 11:12

Maya


People also ask

What is the use of global Asax file in ASP NET?

Adding a Global.asax file is easy: Although optional, the Global.asax file is helpful when creating ASP.NET projects because it allows you to handle events without having to add code to every page of the website. ASP.NET provides developers with an easy way to handle errors using the Global.asax and the easy-to-use Application_Error event.

Where is the Asax DLL located in Visual Studio?

Global.asax is compiled to its own assembly – if you publish the web project from Visual Studio, you can find App_global.asax.dll in the bin directory, otherwise it will go to c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\<appname>. After decompiling the produced assembly,...

What's the correct way of using global_Asax?

So what’s the correct way of using global.asax? First, remember: It’s not a singleton. Application_Start / End events are called only once. There is one instance of global_asax per request and additionally there are instances for special events.

Is global_Asax a singleton file?

It’s not a singleton. Application_Start / End events are called only once. There is one instance of global_asax per request and additionally there are instances for special events. Second, try to apply those guidelines to your Global.asax files:


1 Answers

Server.Transfer needs the actual, virtual path to a resource that will give the output needed. It does not go through IIS to find out what the website's default document(s) are, so it has no clue what you mean by "/Error/" unless that is an actual file name.

Response.Redirect works because that is sending a 'moved' result to the browser with that relative URL (/Error/) and when the browser makes the new request for /Error/, IIS handles it first, and applies the default document settings.

like image 175
Andrew Barber Avatar answered Oct 23 '22 07:10

Andrew Barber