Handling Application_Start Exceptions in IIS7
I am creating an ASP.NET MVC3 application and in addition to the standard MVC exception handling mechanisms. I want to show a static html error page when something went wrong in Application_Start and an unhandled exception was thrown there.
I just added
<customErrors mode="On" defaultRedirect="Error.htm">
and expected redirection to Error.htm in all cases with error. It works correctly with Visual Studio Development Server, but doesn't work with IIS7 (I see a standard yellow screen with "To include details of this particular error message to view on ..."). It seems that when an exception is thrown in Application_Start ASP.NET redirects to Error.htm, at which point IIS calls the Application_Start method again, where the same exception again raises aaaand welcome to an infinite loop.
What are the options for solving this problem? Can I do this without changing IIS settings? If not, how should this be done correctly on the IIS side?
Better late than never. This is the standard way to handle errors:
TestProject \ Views \ Shared \ Error.cshtml
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<p>an error occurred</p><br />
<p>Try again.Go on this <a href="javascript:history.go(-1)">link</a></p>
TestProject \ Controllers \ TestController.cs
namespace testproject.Controllers
{
[HandleError]
public class TestController : Controller
{}
}
TestProject \ Web.config
<system.web>
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml" />
</system.web>