I've tried to fix my error handling for MVC a bit and implemented Marco's solution from here:
ASP.NET MVC 404 Error Handling
This works perfectly on my Win7 workstation. But for some reason it does not work on the server. Certain errors work fine, but for example if I call a controller and route that does not exist I get the standard IIS 404 page.
I've put in some logs and the Error404Controller is called and executed, but for some reason the processing does not stop and it loads the IIS 404 error page afterwards.
This is the code in my global.asax
protected void Application_EndRequest()
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var rd = new RouteData();
rd.Values["controller"] = "Error404";
rd.Values["action"] = "Index";
IController c = new SuperMvc.Controllers.Error404Controller();
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
}
}
And this the controller:
[AcceptVerbs(HttpVerbs.Get)]
public virtual ActionResult Index()
{
log.Error("Error404Controller.Index");
PageError error = new PageError();
error.Url = Request.Url.AbsoluteUri;
log.Error("Request.Url=" + error.Url);
error.UrlReferrer = Request.UrlReferrer.AbsoluteUri;
log.Error("Request.UrlReferrer=" + error.Url);
return View(error);
}
Any ideas? I checked the web.config files and can't find a difference.