当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));
}
}
}
答案 0 :(得分:7)
我添加了它并修复了它。
if (!context.Request.IsAjaxRequest())
{
context.Response.ContentType = "text/html";