I have a custom error page set up for my application:
<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"
/>
In Global.asax, Application_Error(), the following code works to get the exception details:
Exception ex = Server.GetLastError();
if (ex != null)
{
if (ex.GetBaseException() != null)
ex = ex.GetBaseException();
}
By the time I get to my error page (~/errors/GeneralError.aspx.cs), Server.GetLastError() is null
Is there any way I can get the exception details on the Error Page, rather than in Global.asax.cs ?
ASP.NET 3.5 on Vista/IIS7
It worked for me. in MVC 5
in
~\Global.asax
in
~\Controllers
CreateErrorController.cs
in
~\Models
CreateFunctionTools.cs
in
~\Views
Create FolderError
and in~\Views\Error
CreateError.cshtml
If you enter this address
localhost/Error
And if an error occurs
As can be instead of displaying errors, variable 'log' to be stored in the database
This related to these 2 topics below, I want get both GetHtmlErrorMessage and Session on Error page.
Session is null after ResponseRewrite
Why is HttpContext.Session null when redirectMode = ResponseRewrite
I tried and see solution which no need
Server.Transfer() or Response.Redirect()
First: remove ResponseRewrite in web.config
Web.config
Then Global.asax
Then errorHandler.aspx.cs
For references
http://www.developer.com/net/asp/article.php/3299641/ServerTransfer-Vs-ResponseRedirect.htm
I think you have a couple of options here.
you could store the last Exception in the Session and retrieve it from your custom error page; or you could just redirect to your custom error page within the Application_error event. If you choose the latter, you want to make sure you use the Server.Transfer method.
Looking more closely at my web.config set up, one of the comments in this post is very helpful
So we can amend
customErrors
to add this parameter:the
ResponseRewrite
mode allows us to load the «Error Page» without redirecting the browser, so the URL stays the same, and importantly for me, exception information is not lost.One important consideration that I think everybody is missing here is a load-balancing (web farm) scenario. Since the server that's executing global.asax may be different than the server that's about the execute the custom error page, stashing the exception object in Application is not reliable.
I'm still looking for a reliable solution to this problem in a web farm configuration, and/or a good explanation from MS as to why you just can't pick up the exception with Server.GetLastError on the custom error page like you can in global.asax Application_Error.
P.S. It's unsafe to store data in the Application collection without first locking it and then unlocking it.
A combination of what NailItDown and Victor said. The preferred/easiest way is to use your Global.Asax to store the error and then redirect to your custom error page.
Global.asax:
In addition, you need to set up your web.config:
And finally, do whatever you need to with the exception you've stored in your error page: