I know it's a common issue but I've crawled many discussions with no result.
I'm trying to handle errors with the HandleError ASP.MVC attrbiute. I'm using MVC 4.
My Error page is places in Views/Shared/Error.cshtml and looks like that:
Test error page
<hgroup class="title">
<h1 class="error">Error.</h1>
<h2 class="error">An error occurred while processing your request.</h2>
</hgroup>
My FilterConfig.cs in the App-Start folder is:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
My controller:
public class TestController : Controller
{
[HandleError(View = "Error")]
public ActionResult Index()
{
throw new Exception("oops");
}
}
And finally my Web.config in has the following node:
<customErrors mode="On" defaultRedirect="Error">
</customErrors>
When I call the controller action I get a white screen with a following text:
Server Error in '/' Application.
Runtime Error Description: An exception occurred while processing
your request. Additionally, another exception occurred while executing
the custom error page for the first exception. The request has been
terminated.
If defaultRedirect="Error" is not set in the Web.config then I get yellow screen with a following text:
Server Error in '/' Application.
Runtime Error Description: An application error occurred on the
server. The current custom error settings for this application prevent
the details of the application error from being viewed.
Details: To enable the details of this specific error message to be
viewable on the local server machine, please create a
tag within a "web.config" configuration file located in the root
directory of the current web application. This tag
should then have its "mode" attribute set to "RemoteOnly". To enable
the details to be viewable on remote machines, please set "mode" to
"Off".
Notes: The current error page you are seeing can be replaced by a
custom error page by modifying the "defaultRedirect" attribute of the
application's configuration tag to point to a custom
error page URL.
Does anybody know what can be wrong?
EDIT:
Errors were caused by using strongly typed layout. When error is thrown MVC's error handling mechanism is creating HandleErrorInfo object which is passed to the Error view. However if we use strongly typed layout then types doesn't match.
Solution in my case is using Application_Error method in Global.asax, which was perfectly described by the SBirthare below.
See Question&Answers more detail:
os