我在我的项目中实现了自定义错误功能,它在本地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);
}
那我该怎么办?
答案 0 :(得分:6)
我遇到这个问题,即活动IIS服务器上的错误没有显示自定义错误页面(返回正确的HttpStatusCodes)但是它在本地IIS上工作(使用默认网站的本地主机地址 - 不 Cassini) 。它们应该与我想象的完全一样 - 无论如何这个web.config设置修复了它。
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
</system.webServer>
</configuration>
请注意,我的设置使用了global.asax中的Application_Error以及其他web.config设置:
<customErrors mode="On">
<!-- There is custom handling of errors in Global.asax -->
</customErrors>
答案 1 :(得分:1)
2个方法
路线方法
// We couldn't find a route to handle the request. Show the 404 page.
routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "CustomError" } );
或
Web.config中的自定义错误处理程序:
<customErrors mode="On" >
<error statusCode="404" redirect="~/CatchallController/CustomError" />
</customErrors>
无路由匹配引发的条件是404.这样您就可以将所有不匹配指向您的~/CatchallController/CustomError