Controller.Execute rendering as text file with no content type

878 views Asked by At

When a 404 or 500 happens the page shows as a text file. The content type is empty in the response. How can I fix this so the content renders as a "text/html" page.

 protected void Application_Error()
        {
            var context = new HttpContextWrapper(Context);
            if (!context.Request.IsAjaxRequest())
            {
                var unhandledException = Server.GetLastError();
                var httpException = unhandledException as HttpException;
                if (httpException == null)
                {
                    var innerException = unhandledException.InnerException;
                    httpException = innerException as HttpException;
                }

                var routeData = new RouteData();
                routeData.Values.Add("controller", MVC.Errors.Name);

                if (httpException != null)
                {
                    var httpCode = httpException.GetHttpCode();
                    switch (httpCode)
                    {
                        case (int)HttpStatusCode.NotFound:
                            routeData.Values.Add("action", "PageNotFound");
                            Server.ClearError();
                            IController pageNotFoundController = new ErrorsController();
                            pageNotFoundController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
                            break;
                    }
                }
                else
                {
                    routeData.Values.Add("action", "Error");
                    Server.ClearError();
                    IController errorController = new ErrorsController();
                    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
                }
            }
        }
1

There are 1 answers

0
Mike Flynn On BEST ANSWER

I added this and it fixed it.

if (!context.Request.IsAjaxRequest())
        {
            context.Response.ContentType = "text/html";