Controller.Execute呈现为没有内容类型的文本文件

时间:2013-12-05 21:46:18

标签: asp.net asp.net-mvc contenttype

当404或500发生时,页面显示为文本文件。响应中的内容类型为空。我该如何解决这个问题,以便内容呈现为“text / html”页面。

 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 个答案:

答案 0 :(得分:7)

我添加了它并修复了它。

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