loyalflow
loyalflow

Reputation: 14879

Custom error page in web.config, will error info be lost?

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.

Upvotes: 0

Views: 3959

Answers (6)

Gromer
Gromer

Reputation: 9931

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

Upvotes: 1

Jon Hanna
Jon Hanna

Reputation: 113242

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.

Upvotes: 1

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

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.

Upvotes: 0

Ryan McDonough
Ryan McDonough

Reputation: 10012

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

Upvotes: -1

Icarus
Icarus

Reputation: 63956

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.

Upvotes: 0

cjk
cjk

Reputation: 46415

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.

Upvotes: 0

Related Questions