so I was trying to implement a custom 404 page. I used the suggestion from this question to use it and a 404 now successfully loads the wanted view, but somehow the whole page is wrapped up in a pre-tag and thus not rendering. When I request the site manually, it renders just fine, so there might be a problem while reprocessing. The output looks like this:
Output:
<html>
<head>
<html><head><link rel="alternate stylesheet" type="text/css" href="resource://gre-resources/plaintext.css" title="Lange Zeilen umbrechen"
</head>
<body>
<pre><!DOCTYPE html><html>... (my page html)</html></pre>
</body>
</html>
Global.asax
Exception ex = Server.GetLastError();
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
Response.Clear();
...
} else if(ex is HttpException) {
if (((HttpException)ex).GetHttpCode() == 404)
{
routeData.Values.Add("action", "HttpError404");
}
// Pass exception details to the target error View.
routeData.Values.Add("error", ex);
// 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 ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));
}
ErrorController:
[HandleError]
public ActionResult HttpError404(string error)
{
ViewBag.Title = "Page not found! 404";
ViewBag.Description = error;
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View("Index");
}
Update Thanks to M.Babcock, it worked with simply adding Response.ContentType = "text/html"; to my Controller-Method.