I catch and redirect 404 errors to an Error controller \ views with the following code.
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "HttpError";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
//if http 404 redirect to http404 action
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
In the error controller if I set Response.StatusCode = (int)HttpStatusCode.NotFound I get the generic error handler, not the view I returned. This only seems to happen in production. Any ideas why?
[AllowAnonymous]
public ActionResult Http404()
{
//this line causes generic error handler
Response.StatusCode = (int)HttpStatusCode.NotFound;
var viewModel = new ErrorViewModel
{
Description = "Hmm, the page you're looking for can't be found"
};
return View("Error", viewModel);
}