I have a .NET MVC application with which I have problems getting my own error pages working on a server with IIS 8. Throughout my application, I caught and threw exceptions properly and displayed a message on my error page, configured to violation. All this works fine when starting the application through VS during debugging, as well as when setting up the site on my local host in IIS (6.1).
Then I deployed it to the server with IIS 8 installed. Initially, I got an unpleasant default error page 500: default error page 500 http://img202.imageshack.us/img202/1352/b8gr.png
After doing a bit of research, I found that I can add the following to web.config, at least get me my page with a friendly error, although without an individual text:
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
I am executing my own error message using the following filter:
public class GlobalExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.IsCustomErrorEnabled && !filterContext.ExceptionHandled)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = 500;
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo info = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
ErrorController newController = factory.CreateController(filterContext.RequestContext, "Error") as ErrorController;
filterContext.RouteData.Values["controller"] = "Error";
filterContext.Controller = newController;
var model = new ErrorViewModel { Exception = info.Exception };
if (info.Exception.GetType() == typeof(RchiveException) || info.Exception.GetType().IsSubclassOf(typeof(RchiveException)))
model.Message = info.Exception.Message;
else
model.Message = "An error occurred processing your request.";
filterContext.Controller.ViewData = new ViewDataDictionary(model);
string actionToCall = "Index";
if (filterContext.HttpContext.Request.IsAjaxRequest())
actionToCall = "IndexAjax";
filterContext.RouteData.Values["action"] = actionToCall;
newController.ActionInvoker.InvokeAction(filterContext, actionToCall);
}
}
}
Any ideas?
source
share