2

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.

Community
  • 1
  • 1
be_bri
  • 61
  • 6
  • Have you confirmed the mime type received by the browser when your error page is downloaded? – M.Babcock May 26 '16 at 21:33
  • 2
    Open the dev tools in your browser and look at the response headers. The one you're looking for should be named Content-Type I believe. – M.Babcock May 26 '16 at 21:43
  • Nice, that worked for me. Changed the Content-type in the Controller-Method. Thanks! – be_bri May 26 '16 at 21:47

0 Answers0