Mik*_*ynn 4 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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了这个并修复了它.
if (!context.Request.IsAjaxRequest())
{
context.Response.ContentType = "text/html";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
807 次 |
| 最近记录: |