2013-01-24 91 views
1

我在我的项目中实现了自定义错误功能,并且它在本地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); 
} 

那么,应该怎么办?

+2

我建议你提供一些代码让我们来看看.. –

+0

我们可以看到一些码?你的web.config安装是否正确?你还没有给我们很多信息继续下去。 –

+0

本地IIS和Live IIS的含义是什么?我认为你需要检查IIS配置。 –

回答

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> 

条件RAI sed没有路由匹配是一个404.这种方式你直接所有不匹配到你的~/CatchallController/CustomError

+0

但这两种方法都会在浏览器中产生'200 OK'状态,而所需的行为是获得'404'。其实我碰到过这个问题,当Casini在没有路由匹配的时候调用Application_Error,但是IIS Express没有(它显示它的标准404错误报告)。有没有办法在IIS Express中同时获得'HTTP ERROR 404'状态和自定义错误页面(我没有在IIS上检查所有这些)? – horgh

+0

@KonstantinVasilcov,自从我看到这个OP以来,这是一个月。我仍然想着它再次。从现在看,我认为目标是优雅地捕捉404 ...... –

4

我有这个问题,活IIS服务器上的错误没有显示自定义错误页面(返回正确的HttpStatusCodes),但它是WAS在本地IIS上工作(本地主机地址使用默认网站 - 而不是卡西尼)。他们应该工作完全一样,我会想 - 无论如何这个web.config设置修复它。

<configuration> 
    <system.webServer> 
     <httpErrors existingResponse="PassThrough"></httpErrors> 
    </system.webServer> 
</configuration> 

请注意,我的设置使用的Application_Error Global.asax中,只是这个其他的web.config设置:

<customErrors mode="On"> 
    <!-- There is custom handling of errors in Global.asax --> 
</customErrors> 
相关问题