Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller.Execute rendering as text file with no content type

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));
                }
            }
        }
like image 981
Mike Flynn Avatar asked Feb 14 '23 11:02

Mike Flynn


1 Answers

I added this and it fixed it.

if (!context.Request.IsAjaxRequest())
        {
            context.Response.ContentType = "text/html";
like image 169
Mike Flynn Avatar answered Apr 08 '23 08:04

Mike Flynn