State Management :: Using Application_Error For Redirection / Application_Error To Redirect The User To A Custom Error Page?

Oct 11, 2010

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 other? and (b) What available information can I poll to tell me when a given condition exists.

After much searching, I cannot find a reasonable answer to this question. So I began to trap errors and examine the HttpContext object for some indicator. One thing that I was looking for is the reason why if even with a defined Response in the current context does a redirect fail. The only thing that stood out is that even though a Response object may exist, the Session data could be absent. I added the case to check whether the Session data was null and perform a Transfer in this case and it
seems to be handling it properly.

I emphasize "seems to be handling it properly" because no documentation I've been able to find confirms either the problem I am having or whether this is an appropriate strategy for solving it.

I guess the question is, am I on the right track here or is the null Session object just a red herring, indicative of nothing relevant. Here's the check I have set up for reference.


View 2 Replies

Similar Messages:

C# - Application_Error Exceptions Handling, Redirection Infinite Loop And CSS Loading?

Oct 9, 2010

I have error handling in Application_Error event of globals.asax file. Inside this event, I'm using Response.Redirect(~errorview.aspx) method, for redirection to site which is able to handle errors in user friendly way.

Everything works fine, unless exception is rising in Application_Start event. When error occurs there, my application gets trapped in infinite loop with the Application_Error method hit repeatedly. What is more, the page I'm redirecting to never gets hit. Changing Response.Redirect(~errorview.aspx) method to Response.Redirect(~errorview.aspx, false) changes nothing.

The good news is, when Response.Redirect(~errorview.aspx) has been replaced with Server.Transfer(~errorview.aspx), errorview.aspx page succesfully gets hit.

The side effect now is not loading CSS, and errorview.aspx page looks ugly. What is more, the CSS is not loaded only when exception occurs in Application_Start event. Exceptions thrown from any other place don't make the CSS mess.

How I can handle this problem in correct way, and why the CSS is missing in the one particular situation ? What is the appropriate way of handling errors in my case ?


For CSS loading, I'm using this:

<link href="~/Css/Layout/style.css" type="text/css" rel="stylesheet"
runat="server" ID="_uid" />

error page I'm transfering from: http://localhost/APP/Pages/Module/Pages/ErrorView.aspx

css folder path: http://localhost/APP/Pages/Module/CSS/Layout/style.css

View 2 Replies

.net - Redirect In Application_Error Redundant If Using CustomErrors?

Jun 9, 2010

If I have a customErrors section in my Web.config that says to redirect to Error.html, then putting code in the Application_Error method in the Global.asax to redirect to Error.html is redundant is it not? Technically, I could bypass the Web.config by redirecting to a different page in the Application_Error method if I wanted to, but since I don't want to go to a separate page I don't think I need the code.

View 2 Replies

Web Forms :: Response.Redirect Does Not Work After Called From Application_Error

Nov 23, 2010

I've followed the instructions in the two articles above, plus more and I still cannot get response.redirect to work. I am able to see the url of the page I am trying to redirect to in fiddler but the browser does not navigate to the page. I am using Cassini as a web server, could that be the problem?

Here is global.asax


View 5 Replies

Exceptions - Catch In Page Or Handle In Global.asax (Application_Error)

Jan 27, 2011

Looking for best practice focused answers here with explanations.

Should the presentation layer of an ASP.Net app catch and handle exceptions thrown from the business layer, or should these be allowed to bubble out, where they can all be logged and handled uniformly in the Global.ascx's Application_Error handler?


View 2 Replies

State Management :: Redirect User To Login Page If Session Is Null?

Sep 7, 2010

i have 2 page,

1.login.aspx --- 2.welcome.aspx

in my login.aspx im storing username into sesssion as below:

Session["usernm"] = txtUsername.Text;
in my welcome.aspx page
in page_load event
if (Session["usernm"]==null)

here if i manulally paste the url ( http://localhost:4125/Loginado/welcome.aspx) it should go to login.aspx for user credentials

but its going directly to welcome.aspx page.

View 5 Replies

Getting Redirection To Custom Error Page Using Custom Errors

Mar 24, 2010

Here's my Application_OnError event sink in global.asax.vb:

Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Dim fourOhThree As Integer = DirectCast(HttpStatusCode.Forbidden, Integer)
Throw New HttpException(fourOhThree, innerMostException.Message, innerMostException)
End If
End Sub

You'll see that if we've got an innermost Exception of type AccessDeniedException we throw a new HTTPExcpetion with a status code of 403 AKA 'forbidden'

Here's the relevant web.config entry:

<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On">
<error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />

So what we're expecting is a redirect to the AccessDenied.aspx page. What we get is a redirect to the ServerError.aspx page.

We've also tried this:

Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Context.Response.StatusCode = DirectCast(HttpStatusCode.Forbidden, Integer)
End If
End Sub

Which unsuprisingly doesn't work either.

View 1 Replies

Custom Error 404 Page Redirection Without File Extension?

Apr 16, 2010

I need to set Custom Error Validation for my site.

I have given [URL] .This says 404 Error.

I already set it my web.config file for 404 error redirection.But it is not working.

View 6 Replies

Configuration :: Application_Error Is Not Executing?

Feb 21, 2011

I have writen error handling code in my Application_Error event in global.aspx page, but that event is not executed any time when error is occurring.So do I need to take care any other settings on my application to run this.

View 2 Replies

Application_Error In Global.asax Not Firing?

Oct 8, 2010

I'm trying to handle all my application exceptions inside the global.asax but from some reason it's not firing at all, i never tried to do it before so i guess i'm doing something wrong here...

here is the code inside the default.aspx page that throw the error:


View 6 Replies

Clearing Session In Global Application_Error?

May 10, 2010

Whenever an unhandled exception occurs on our site, I want to:

Send a notification email Clear the user's session Send the user to a error page ("Sorry, a problem occurred...")

The first and last I've had working for a long time but the second is causing me some issues. My Global.asax.vb includes:


When I run a debug, I can see the session being cleared, but then on the next page the session is back again!

I eventually found a reference that suggests that changes to session will not be saved unless Server.ClearError is called. Unfortunately, if I add this (just below the line that sets "ex") then the CustomErrors redirect doesn't seem to kick in and I'm left with a blank page?

View 2 Replies

File Not Found Exception In Application_Error

Nov 30, 2010

i am new in mvc. Sometimes i have an exception in Application_Error and i write it to my log file:


I trying debug but i not see file name? How to see witch file cannot be found ?

View 1 Replies

C# - Application_Error No Longer Triggers When Published To IIS?

Oct 22, 2010

I have the following code in my Global.asax file:


To provide neat and tidy error urls like "/Error/404/TheNameOfTheRequestedPage". This works fine from VS 2008, but once published to my local machine, I get the default error page:

Error Summary

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or
is temporarily unavailable

I've chosen not to use system.web/customErrors because I don't have access to Server.GetLastError() from there (or at least it's never worked for me) and I want to get the http code.

View 2 Replies

C# - Application Keeps Running Even After Application_Error Finishes?

Jun 4, 2010

the application keeps running even after Application_Error event is called, is there any way to terminate the application in that event itself

View 2 Replies

Web Forms :: Application_Error Fires On Startup?

Nov 28, 2010

When I start debugging my application using vs2010 before the default.aspx page is rendered the application_error event is fired in global.asax.

There is nothing happening in the page_load of the default.aspx screen.However, if I set a break point on the default.aspx page_load event and the application_error event here are the sequence of events: The page_load event of default.aspx is fired, there is no code to execute there so I hit F5 to continue. The application_error event is then fired.Here are some of the details from Server.GetLastError();

exception message is "File does not exist."

stack trace:

at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)

I have absolutely no idea why this event is fired. Do all of the events in the Global.asax get fired on startup?There is nothing happening on the page_load so how can an error be thrown?

View 7 Replies

State Management :: Redirect User After Timeout?

Oct 8, 2010

How can I redirect the user to the first page when they've been idle for more than 20 minutes or so?Here's my session timeout time in my web config file:


View 6 Replies

Web Forms :: How To Purposely Raise An Application_Error In Global.asax

Nov 14, 2010

Just for kicks, how do you say, wire up an ASP button to purposely throw an Application_Error exception in the global.asax file?

lets say you wanted to test the functionality of the error handling and what is doing..and you don't have a real error to do it. what is the easiest way just raise this event?

View 4 Replies

Configuration :: Application_Error Problem With Request Ending In Period/dot?

Apr 13, 2010

I was testing out my custom error handling specifically around 404s when I came across an intersting problem, which I recreated on a new web application.

Create web application
Create a global.asax file
Create a new web form - error.aspx - "This is the error page text"


View 3 Replies

Application_Error, CustomErrors Mode On, Try Catch ... Dont Want To Bubble Up?

Jan 22, 2010

I have the following settings. 2.0 / 2005 / Web.config -> customErrors mode="on"

<customErrors mode="On" defaultRedirect="~/ErrorPages/Default.aspx">

View 6 Replies

Web Forms :: Exceptions From Business Logic Layer Not Caught In Application_Error?

Nov 11, 2010

I know there are a few posts on this issue already, however I haven't found the answers I was really looking for.

My situation is like this: I have a DLL project containing my business logic. Then I have a web application that refers to this DLL, and calls a function from it. And I have a global.asax which handles errors on Application_Error


// MyWebsite.aspx.cs
using MyBusinessLogic;
protected void Page_Load(object sender, EventArgs e)
// global.asax.cs
protected void Application_Error(object sender, EventArgs e)


View 6 Replies

Configuration :: Handling Errors In Global.asax - Application_Error Fires Twice

May 25, 2010

I am handling errors in Global.asax. When I encounter an error in certain pages, the Application_Error event fires twice. On the second trip, Server.GetLastError() returns null. I have a simple page where I throw an error in page load. When that page throws an error the code below works fine. I also have a more complex page with an update panel and many controls. When that page throws an error Application_Error fires twice. I suspect the update panel but update panels are always suspect, right?

There are no try/catch blocks on any of my pages I'm testing with. Custom errors in web.config is commented out. Note that logging and busywork in the Application_Error event are commented out. I'm pretty much just doing a respone.redirect. The problem with the the event firing twice is that the response.redirect fails with an error msg stating that headers have already been sent (adding Context.ClearError and Response.Clear does nothing to fix that).

View 20 Replies

HttpHandlers / Modules :: Getting Exception Info From Catch Block To Application_Error Event Of HttpModule?

Oct 7, 2010

regarding one issue I am facing. I created a httpmodule for hadling the unhandled errors and I need to log those errors using log 4 net in database.Everting working fine but I am not getting any error information once the error was hadled and I need that info too. I want to make it as a centralize module which can trap all errors and log those errors.How call application_error event of httpmodule from catch that I can log that info there and I no need log in every catch block..

View 1 Replies

State Management :: Redirect Page To Login Page On Logout?

Dec 2, 2010

In logout link's click event i have written Session.Abandon() that will destroy the Session state.I am also handling Session_End event that is fired afterward (written in Global.asax). But one more thing i want is that after Session ends, login page should be redirected (opened).

For that i wrote:


This is working fine on the surface, but after applying debugging i saw that there were unexpected number of calls to Session_Start and Session_End events for two times, then login page was opened.

Why Session_Start and Session_End was called two times ? If i left this process as it is working then may i get some serious error in future ?

View 4 Replies

State Management :: Session Time Out - Redirect Page To Default

Feb 21, 2011

I'm using sessions to track user name. I assign Session["userName"] in my login procedure. The sessionn gets timeout in about 15 minutes. What I need to do is, if the session timeouts I need to redirect the page to the Default.aspx page.

How can I do this? Any code example would be great.

View 7 Replies

State Management :: Custom Session Provider Cause Serialization Error

Jul 19, 2010

I use custom session state provider (appfabric). I marked my pagecontroller class with [Serializable]. When i redirect a page i get error HttpContext is not marked as serializable. I have a property in pagecontroller class;


Is this piece of code may be the main reason for serialization error ? I use the it nearly everywhere, so i can not simply detach and test it.

View 3 Replies

Copyrights 2005-15, All rights reserved