Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET 4.0 Webforms - global error handling troubles
    primarykey
    data
    text
    <p>I'm back in ASP.NET land after a longer Winforms hiatus - and I'm back at struggling with stuff that is <em>supposed</em> to work - but really isn't....</p> <p>I'm trying to implement a global ASP.NET error handler and a page to show the error to the developer (in a DEV environment).</p> <p>In <code>global.asax.cs</code>, I catch unhandled errors like this:</p> <pre><code>private void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex != null) { HttpContext.Current.Items["exception"] = ex; // do a bit more stuff here, like logging the error and so on Server.Transfer("~/Error.aspx"); } } </code></pre> <p>(<em>PS: as a side-note: is using <code>Server.GetLastError()</code> or <code>HttpContext.Current.Error</code> the preferred way to go? Cannot really find any hard facts on what's better to use, and why</em>)</p> <p>and in my "dev" error page, I'm trying to show the error on screen - but trying to get that error seems elusive - I only ever get back <code>null</code>...</p> <pre><code>public partial class Error : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // both "exc" as well as "exc2" always end up being "null" ... Exception exc = HttpContext.Current.Error; Exception exc2 = HttpContext.Current.Items["exception"] as Exception; // display error ..... } </code></pre> <p><strong>Why</strong>? I was assuming that if I use <code>Server.Transfer()</code> that error would still be there (I'm never calling <code>Server.ClearError()</code>), and if that doesn't work, then I would expect that at least sticking the exception into <code>HttpContext.Items</code> would work since that's still code running inside the same HTTP context/request - no?</p> <p>What am I missing here? And how <em>can I</em> "transport" exception information from catching it in <code>global.asax.cs</code> to a separate error page otherwise?</p> <p>Thanks for all hints and pointers !</p> <hr/> <p><strong>Update:</strong> OK - seems this doesn't happen always - in some cases, this mechanism works just fine (as I had expected it to work).</p> <p><strong>BUT:</strong> I have a grid on a page that contains <code>ImageButton</code> for actions like edit and delete. I have defined a <code>MyGridViewHandleRowCommand</code> event handler to handle those commands - but that doesn't seem to work. Whenever I click on one of those image buttons, my error page gets activated - and yes, the code seems to <strong>totally bypass</strong> my global <code>Application_Error</code> handler in <code>global.asax</code>! And in this case, I cannot get any information about the error in the error page - everything is null .....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload