I have the following code which handles all execptions that occur outside of action results (where the HandleErrorAttribute cannot reach).
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = (HttpException)exception;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpException == null)
{
routeData.Values.Add("action", "Home");
}
else //It's an Http Exception, Let's handle it.
{
routeData.Values.Add("action", "Handle");
}
// Pass exception details to the target error View.
routeData.Values.Add("error", httpException);
// Clear the error on server.
Server.ClearError();
// Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController errorController = new SPP.Areas.UI.Controllers.ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
This does the job just fine. It executes the "Handle" action on the "Error" controller just like I have set it. No problem.
When inside the action it then throws another exception which is then picked up by my HandleErrorAttribute filter. After debugging it I can clearly see it is looking for the following paths.
- ~/Views/Error/Handle.aspx
- ~/Views/Error/Handle.ascx
- ~/Views/Shared/Handle.aspx
- ~/Views/Shared/Handle.ascx
- ~/Views/Error/Handle.cshtm
- ~/Views/Error/Handle.vbhtml
- ~/Views/Shared/Handle.cshtml
- ~/Views/Shared/Handle.vbhtml
I am using the Razors view engine along with Areas. My reason for doing this. So I can keep my application nice a tidy. My problem is that it looks like I am being forced to create a directory within my application just so this file can sit inside it...I am not a fan of this.
I would much rather prefer it going to the following location.
- ~/Areas/UI/Views/Error/Handle.cshtml
Or something like...
- ~/Areas/Shared/Views/Error/Handle.cshtml
Now I CAN resolve this by creating that directory at the root of the application.
The reason I have not done this is because if I start caving in and using an easy way out approach. My applications will get very messy very quickly. I like to be the one who defines where my files and folders live.
If anyone knows of a way to set the direct path, or tell the IController (errorcontroller) to look in the Area in which it has been instanciated I will love you forever.
Any help will be much appreciated.
Steve