0

If my web.config has:

<customErrors mode="Off" defaultRedirect="CustomErrorPage.aspx">
      <error statusCode="401" redirect="~/CustomErrorPage.aspx?statusCode=401" />
      <error statusCode="403" redirect="~/CustomErrorPage.aspx?statusCode=403" />
      <error statusCode="404" redirect="~/CustomErrorPage.aspx?statusCode=404" />
      ...
</customErrors>

Now in my CustomErrorPage.aspx, how can I get the stacktrace information similar to how I see that yellow screen error page when there is no custom error page and it is outputted to the browser?

Or, because this is redirecting to the customerrorpage.aspx, is the error essentially lost at this point and I can't access the exception information?

This is a legacy application with complex virtual directories etc. so I can just drop one of those error libraries so easily at this point.

loyalflow
  • 14,275
  • 27
  • 107
  • 168

6 Answers6

1

Per this SO post (http://stackoverflow.com/questions/343014/asp-net-custom-error-page-server-getlasterror-is-null), if you are running .Net 3.5 SP1, you can use the redirectMode property on your customErrors element.

More info on the redirectMode: http://msdn.microsoft.com/en-us/library/system.web.configuration.customerrorssection.redirectmode(v=vs.90).aspx

Once you do that, you'll have access to the error from Server.GetLastError(): http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.getlasterror.aspx

Edit: You'd be using the ResponseRewrite mode for redirectMode

Gromer
  • 9,861
  • 4
  • 34
  • 55
1

It's lost. You aren't even in the same request, since it's done by a redirect.

That last point is bad enough in itself (what's the point of redirecting someone to an error page?), but it affects you here. However, with redirectMode="ResponseRewrite" added to the customErrors element, then that solves this problem and also means that Server.GetLastError() will work.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

You can log error in some file on server (you need to add Application_Error event handler in Global.asax.cs), or drop this row in webconfig to get yellow screen with exception.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
0

In your global.asax you can catch the error and save it to be retrieved in the Error page.

You override Application_Error to catch the error.

cjk
  • 45,739
  • 9
  • 81
  • 112
0

The Exception is not lost. You can call Server.GetLastError() to get the last exception thrown.

Since it returns an Exception object, you can simply get call StackTrace to get the full stack trace.

Icarus
  • 63,293
  • 14
  • 100
  • 115
-1

Use GetLastError, that allows you (oddly enough) to get the last error that occurred.

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.getlasterror.aspx

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76