.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Using Application_Error for Redirection

Posted By:      Posted Date: October 11, 2010    Points: 0   Category :ASP.Net

I am trying to use the Application_Error to redirect the user to a custom error page.  I know I could use the web.config to do this, but let's assume for now that this is not a desirable path to take.

Initially, I tried to do a Response.Redirect to perform this, however, there seem to be occasions where the current http context does not define the response object.  So, I attempted to perform a check to make sure that the response object is not null prior to attempting the redirection, and if it is not defined, perform a Server.Transfer instead.

What happens is that in most cases, the Redirect causes the browser to some generic "friendlyish" error page rather than the requested error page.

So then I tried using Server.Transfer exclusively, and this worked well for most cases, however, sometimes the transfer didn't seem to take in the browser.  Changing it back to a redirect in these cases solved that problem, but reintroduced the issue where the errors previously being captured and transferred now were being redirected.

What I surmised from this is that in certain contexts where the Application_Error method is trapped, it is necessary to use Server.Transfer in redirection, whereas in others, it is necessary to use Response.Redirect.  The question then became twofold: (a) When is one necessary and when is the ot

View Complete Post

More Related Resource Links

Mobile detection and redirection


Hi all,


I want to check with you what things to add  to implement a mobile browser and then redirect it to other site.

Do I have to add Browsercap in the web.config?

also I want you to help me with this script that i coded (not a programmer):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
    // SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)

        if (Request.Browser.Browser == "AppleWebKIt" )

HTTP Pipelines: Securely Implement Request Processing, Filtering, and Content Redirection with HTTP


ASP.NET is a flexible and extensible framework for server-side HTTP programming. While most people think of ASP.NET in terms of pages served, there is a lower-level infrastructure sitting beneath this page model. The underlying plumbing is based on a pipeline of app, module, and handler objects. Understanding how this pipeline works is key if you want to get the most out of ASP.NET as an HTTP server platform, while making your process more efficient, and keeping your server secure. This article introduces the architecture of the pipeline and shows how you can use it to add sophisticated functionality to an ASP.NET-based app.

Tim Ewald and Keith Brown

MSDN Magazine September 2002

New Directions in Redirection: Microsoft Internet Information Services 5.0 Provides Two New Methods


Internet Information Services (IIS) 5.0 provides several enhancements to its support for ASP-based Web development, including two new server-side redirection methods: Server.Transfer and Server.Execute. Rather than redirecting requests with a round-trip to the client, these new methods can be used to transfer requests directly to an ASP file without ever leaving the server. While this functionality doesn't replace the Response.Redirect method used by IIS 4.0, you can take advantage of it to implement better application flow control mechanisms and to handle errors more efficiently. The different redirection options are described, along with some tips and tricks for implementing them on your own site.

Ram Papatla

MSDN Magazine April 2000

Login Redirection



i have creaated my own login control and my site have many pages many of pages are public to anyone but when they try to enter some section or try to send sms they be redirect to login page

my problem now after tey redirect to login page  and enter login info they redirect back to Default.aspx not the previous page they want to enter


user request page named  Page1.aspx  /page one user have to be registered so he will redirect to Login.aspx

user will enter sign in info   so he will redirect to Default.aspx /that's my problem

cause what i want to do ,that user will redirect back to Page1.aspx

how can i do this

help please


redirection from _layouts/reqacc.aspx



I'm working on SP 2007 on client side only (no access to server).

I would like to redirect the _layouts/reqacc.aspx path from spacific portal without chang this page.

Actually, i would like the make my own page.

Since i have no permissions on server, i need to redirect locally on my portal, but i can find where.

Any idea????


POSTing to a remote site and HTTP 302 redirection

hello everyone, Objective: POST data to a remote website and follow the 302 redirect response Synopsis: This works quite well in plain ASP.NET. I can POST to the remote site either using the asp form's action attribute or a button and it's PostBackUrl property. However as soon as I try to accomplish the same behavior inside a Sharepoint page it causes a lot of trouble. Scenario 1 (changing the form's action attribute - less preferred): Imagine this simple publishing aspx page (for what it's worth I encountered the exact same behavior using an application page). <input type="text" name="myField1" value="abc" /> <input type="text" name="myField2" value="abcdef" /> <input type="text" name="myField3" value="123" /> <input type="submit" value="submit now" /> Note that I need to use pure html controls because the remote site expects pre-defined names for these fields. And as we all know control naming is something we cannot really influence up to .NET framework 3.5SP1 (4.0 is supposed to help, right?). Anyway we still need to set the action attribute of the form. This is being done in the page's OnLoad() method. Result: The aspform is being posted to the remote site, the remote site responds with 302 and the browser redirects to the returned ur

Customize SSIS Error row Redirection

Is there a way I can get actual field values for Error Rows while redirecting the error rows to a flat file.  I  have a data flow task with Oledb Source and flat file destination.  I configured the Flat File Source Error Output and am only getting the ErrorCode and ErrorColumn in the error output for "Text was truncated error"   -1071607675, 22180   But i'm not sure which column in which row resulted in this truncation error. Is there a way I can pinpoint the error Source?     thanks.

Application_Error not firing on Production server.

 Hi, I am trying to setup my application on Production server. Whenever any error occurs, we are redirecting to a specific page in Application_Error Event in Global.asaxThe code in Application_Error Page_________________________________________ string _rawUrl = Request.RawUrl;if (_rawUrl.IndexOf(".aspx") == -1 && _rawUrl.IndexOf(".gif") == -1 && _rawUrl.IndexOf(".jpg") == -1){       string[] _arrRawURL = _rawUrl.Split('/');       Server.Transfer("~/UserHomePage/Default.aspx?UserProfile=" + _arrRawURL[_arrRawURL.Length - 1]);}  It is getting fired Locally. As it was not working, I tried the alternative as belowMade Custom Errors to On in Web.config. and in IIS, in Custom Errors section, under 404 Page I gave the ErrorPage file Name and written the above code. But still  it is not working.Kindly help me out!  Thanks in Advance. Chaitanya.

SPD Workflows - Assembly redirection doesn't work

Hi, I'm migrating a solution from MOSS2007 to SharePoint 2010. I have, among others, custom workflow actions in a DLL deployed to the GAC (e.g. MyWorkflows.dll version My new solution contains new workflow actions that target the SharePoint 2010 API (e.g. MyWorkflows.dll version So what I'm doing is: - Creating a workflow in SPD in a MOSS2007 environment. Such workflow contains one of my custom actions. - Upgrading the environment to SharePoint 2010. - Removing the WSP package containing my files/DLLs that targeted MOSS2007 and deploying a new WSP containing the solution files that target SharePoint 2010. - Add assembly redirection entries in the web.config file of my web application, so that calls to MyWorkflows.dll get redirected to MyWorkflows.dll version (I also add other redirection entries regarding other stuff - for example, webparts). - Reset IIS and OWSTIMER.   After that I can see that the old webparts work fine (the assembly redirection seems to work for them). However, when I try to run a workflow (manually, from the browser) it doesn't work. I get the following error in the ULS log: Microsoft.SharePoint.SPException: <Error><CompilerError Line="15" Column="3" Text="Could not deserialize object. The type 'MyWorkflows.MyActivity' could not be resolved." /></Error>   &nbs

How to catch error in session variable in globals.asax(Application_Error) and redirect to same page

Hi, i have a login page where username got to enter his/her username or password. When you try to enter any scripting over there eg:'<asd>' it gives an error  'Server Error in '/' Application' 'A Potentially dangerous Request. Form value was detected from client' I don't want to redirect user to custom error page when such unpredictable error occurs. infact i got a label on the same page itself where error's are displayed. how can i display such errors on this very same label. can i do that using globals Application_Error ?. Session variable not working here. Please give me the exact steps for how i can catch and display the error which are not validated. waiting for reply thanks and regards, Learning Programmer

Data Mirroring and Transparent Client Redirection

From what I have read, later versions of MDAC recognize if a database is mirrored and it caches both the primary and the mirror name.  Then if a connection fails to the primary, MDAC transparently redirects to the mirror (which in theory has taken on the primary role).  Could someone explain how applications which rely on a DSN with a specific SQL server name defined can be redirected transparently to another SQL server that is hosting the now "promoted" (formerly a mirror) primary database?  The documentation I have read is high level and just implies it will magically work but I am looking for a deeper explanation of how transparent client redirection works from the application db connectivity perspective.   Thanks, Steve

Application_error not firing

I have included code in the Application_Error event of my global.asax file. Unfortunately it appears that this event is not firing. If I try to handle the event in web.config it works fine. Q. Do I need to set custom errors mode to 'off' in web.config in order for Application_Error to handle the error. If not any ideas why Applicaiton_Error is not running it's code?

Survey summary and redirection to diffrent page

Hi I'm using standard survey list to gather some opinion around diffrent stuff from the users. Few days ago someone from the bussiness came to me and ask if this is possible to redirect user to some page once he submit his vote, so he will not be redirected to default "overview" page. I was trying to use "?Source={URL}" parameter to achive that but this seems to not work. Does anyone has some idea how to overcome this ? thanks in advance Mariusz Gorzoch Blog : http://mariusz.gorzoch.pl Twitter : http://twitter.com/MariuszGorzoch Facebook: http://www.facebook.com/mariusz.gorzoch

Application_Error event handler

I am using Application_Error in Global.asax to capture all exceptions thrown. I log the exceptions and redirect to a single error page. Meanwhile my <customErrors> has mode="off". I believe my application is secure from showing an exception page, including a 404 error. So far, everything shows my error page. I'd like to know if the experts consider this secure.  // this is pseudo code, not the actual code of my site protected void Application_Error(Object sender, EventArgs e) { LogException(HttpContext.Current.Server.GetLastError()); HttpContext.Current.Response.Redirect("~/Error.aspx"); }  

SSP Redirection Problem


Here's the scenario:

I have a SharePoint site with Default and Extranet zones. https://server.domain.tld/ is the Public URL for the Default zone and is protected with Windows Authentication.  https://server.domain.tld:7780/ is the Public URL for the Extranet zone and is protected with Forms-Based authentication.  I configured alternate access mappings so that http://server:9780/ is the internal url for the Extranet zone, and https://server/ is the internal url for the Default zone.

If I click on the My Settings link in the Default zone (i.e. on https://server.domain.tld), then I go to the My Settings page.  The URL is still https://server.domain.tld/_layouts/userdisp.aspx.  But if I click on the My Settings link in the Extranet zone, then I get redirected to the Shared Service Provider.  I want the extranet site to behave the same way; i.e. I do not want a redirect to the Shared Service Provider, but I don't know how to configure that to work properly.  Thanks.

ItemCheckingIn Redirection running differs based on CheckOut Method


Requirement: To redirect to another page after checking in

Using the following code in ItemCheckingIn event of SPItemEventReceiver.

public override void ItemCheckingIn(SPItemEventProperties properties)

 SPUtility.Redirect ("http://site/XXXX.apsx", SPRedirectFlags.Trusted, CNT_CONTEXT);

Issue: During Check in , the way in which the dedirection is displayed differs when the file is checked out locally(To Local Drafts folder) against when checked out from the server.

  • When checked out locally, the validation message is displayed in a JS pop up.
    • This is after the Yes/No popup with message like "You are about to upload <file name>. Do you want to continue?"
    • Also I see another popup after the validation message one , with text like "The file could not be checked in. It may have already been checkedin by another user." 
    • The redirection is not running, just stay the check-in page.
  • When checked out on the server, it can redirct to another page after check

URL Redirection


I am not sure what you call it but this is what i want

Normaly URL are like http://website.com?Date=06-10-2010

but i want it to be like http://website.com/2010/10/06 but at the same time i would like to read the date value from the query string how to go about it?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend