【发布时间】:2013-01-24 05:32:11
【问题描述】:
我已经在我的项目中实现了自定义错误功能,它可以在本地 IIS 上运行,但不能在实时服务器上运行。我已经使用 Global.asax 文件实现了这个功能,并且我在 MVC 的自定义错误控制器中调用了我的自定义错误操作方法。我已经在本地 IIS 上发布并运行,它运行良好,但在实时服务器上。
我的 Global.asax.cs 文件
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//do not register HandleErrorAttribute. use classic error handling mode
filters.Add(new HandleErrorAttribute());
}
protected void Application_Error(Object sender, EventArgs e)
{
LogException(Server.GetLastError());
CustomErrorsSection customErrorsSection = (CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors");
string defaultRedirect = customErrorsSection.DefaultRedirect;
if (customErrorsSection.Mod e== CustomErrorsMode.On)
{
var ex = Server.GetLastError().GetBaseException();
Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", "Common");
routeData.Values.Add("action", "CustomError");
if (ex is HttpException)
{
var httpException = (HttpException)ex;
var code = httpException.GetHttpCode();
routeData.Values.Add("status", code);
}
else
{
routeData.Values.Add("status", 500);
}
routeData.Values.Add("error", ex);
IController errorController = new Test.Controllers.CommonController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
我的自定义错误控制器及其操作方法
public ActionResult CustomError(int status, Exception error)
{
var model = new CustomErrorModel();
model.Code = status;
model.Description = Convert.ToString(error);
Response.StatusCode = status;
return View(model);
}
那我该怎么办?
【问题讨论】:
-
我建议你提供一些代码给我们看看..
-
我们能看到一些代码吗?您的 web.config 设置是否正确?你没有给我们太多信息。
-
本地 IIS 和实时 IIS 是什么意思?我认为您需要检查 IIS 配置。
-
你解决了吗?实际上我遇到了这个问题,当 Casini 在没有匹配路由时调用 Application_Error ,但 IIS Express 没有(它显示其标准 404 错误报告)。有没有办法在 IIS Express 中同时获取 HTTP ERROR 404 状态和自定义错误页面,我想知道(我还没有在 IIS 上检查所有这些)?
标签: c# .net asp.net-mvc global-asax custom-errors