Last post Jun 15, 2018 06:50 AM by b3hdad
Jun 14, 2018 01:33 AM|b3hdad|LINK
I am using MVC 5 and have configured my project to use Autofac. Things are working fine...mostly.
Now I have configured my web.config to handle errors like this:
<customErrors mode="On" redirectMode="ResponseRedirect">
<error statusCode="400" redirect="~/Error/BadRequest" />
<error statusCode="403" redirect="~/Error/BadRequest" />
<error statusCode="404" redirect="~/Error/NotFound" />
<error statusCode="500" redirect="~/Error/Index" />
So what happens is that if I forget a registration of a class in autofac container, I will be taken to one of the above pages, however, I don't see the actual error message to print.
In my error templates error view I define @model HandleErrorInfo at the top and in the body I try to access the error using @Model.Exception.Message. But @Model.Exception.Message throws an exception with null object reference.
What do I need to do to have access to the exception?
Jun 15, 2018 02:48 AM|Nan Yu|LINK
When you use <customErrors> ASP.NET is unaware of how to pass the exception information into a view, it'll just load/redirect you to the error page. You can try below solution to send error model to view from controller :
Jun 15, 2018 06:50 AM|b3hdad|LINK
Hi Nan Yu,
Thank you for the link and information. It was quite helpful. My error handling has some minor similarities with theirs. However, I did take some pointers from their code.
For example I wasn't doing Server.ClearError() and also the line that does:
var httpException = exception as HttpException;
In the Application_Error was setting the httpException to null even though there was a legitimate exception in var "exception". So if httpException is null but exception is not, I will
redirect to error page with the exception's message. It seems to work.