2014-03-19 21 views
0

如何在web.config中配置httpErrors元素以基于抛出的特定HTTP错误显示通用错误页面?配置httpErrors元素以显示通用错误页面

我使用的是ASP.NET MVC 4.NET 4.5

我已经在我的web应用程序的根目录下的以下文件:

403.html 
404.html 
500.html 

我在我的web.config以下设置:

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <httpErrors errorMode="Custom"> 
      <remove statusCode="403" /> 
      <remove statusCode="404" /> 
      <remove statusCode="500" /> 
      <error statusCode="403" path="403.html" responseMode="File" /> 
      <error statusCode="404" path="404.html" responseMode="File" /> 
      <error statusCode="500" path="500.html" responseMode="File" /> 
    </httpErrors> 
</system.webServer> 

我测试上面做类似的东西这个:

throw new System.Web.HttpException(403, "403"); 

这对我来说很完美,正确的HTML文件显示基于其中之一上面提到的HTTP错误。

我想离开上述HTML文件,而是显示在我的Views/Shared文件夹中找到的通用错误页面。它被称为Error.cshtml。我不知道如何改变上面显示这个新的错误页面?

我已经设置了existingResponseAuto,ReplacePassThrough但它只是显示一个白页。我已将path更改为诸如path="/Error"path="Views/Shared/Error"之类的几个变体,这也行不通。

+0

你尝试创建一个errorcontroller /动作,返回错误查看和更改路径,行动? –

+0

是的,但我需要这样做,以便我可以迎合IIS错误。 –

回答

0

您需要为此定义一个控制器。你的行为可以很简单

public ActionResult Error() 
{ 
    return View("~/Views/Shared/Error.cshtml"); 
} 

但是你仍然需要它,如果你想使用ASP.NET MVC机制来显示错误页面。

通过上面的操作定义了控制器(可以说控制器名称也是Error)后,您可以像编写任何其他ASP.NET MVC页面一样编写url:Error/Error

更新。下面是一个例子配置:

<httpErrors errorMode="Custom" 
      defaultResponseMode="ExecuteURL" 
      defaultPath="/Error/Error> 
</httpErrors> 
+0

谢谢。你能告诉我你的httpErrors会是什么样子吗? –

+0

@BrendanVogt,请参阅更新。 – Andrei

+0

默认情况下,Web应用程序在Views/Shared中有一个名为Error.cshtml的文件。我想用这个。这可能吗? –

0

您可以捕获所有的错误,在Global.asax中

protected void Application_Error(object sender, EventArgs e) 
     { 
      var httpContext = ((MvcApplication)sender).Context; 
      var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); 
      var currentController = " "; 
      var currentAction = " "; 
      if (currentRouteData != null) 
      { 
       if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) 
       { 
        currentController = currentRouteData.Values["controller"].ToString(); 
       } 

       if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) 
       { 
        currentAction = currentRouteData.Values["action"].ToString(); 
       } 
      } 
      var ex = Server.GetLastError(); 
      var url = Request.RawUrl; 
      var controller = new ErrorController(); 
      var routeData = new RouteData(); 
      var action = "Error"; 
      httpContext.ClearError(); 
      httpContext.Response.Clear(); 
      httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500; 
      httpContext.Response.TrySkipIisCustomErrors = true; 
      routeData.Values["controller"] = "Error"; 
      routeData.Values["action"] = action; 
      controller.ViewData.Model = new NameSpace.HandleErrorInfo(ex, currentController, currentAction); 
      ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); 
     } 

在此之前建立一个模型,用于stroing错误信息:

public class HandleErrorInfo 
    { 
     public string ActionName { get; set; } 
     public string ControllerName { get; set; } 
     public Exception Exception { get; set; } 
     public HandleErrorInfo(Exception exception, string controllerName, 
      string actionName) 
     { 
      this.ActionName = actionName; 
      this.ControllerName = controllerName; 
      this.Exception = exception; 
     } 
    } 

错误浏览:强烈的双以上述错误模式结束。这将显示所有错误:

@model NameSpace.HandleErrorInfo 
@{ 
    ViewBag.Title = "Error"; 
    Layout = null; 
} 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Error Page</title> 
    <link href="@Url.Content("../../Content/site.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("../../Content/css/menu.css")" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
      <div class="container"> 
      <div> 
       <table id="tblError" visible="false" cellspacing="2" cellpadding="2" width="90%" 
        border="0" style="margin-left: 50px"> 
        <tr> 
         <td align="center" class="pageTitle"> 
          Application Error 
         </td> 
        </tr> 
        <tr> 
         <td style="font-family: Arial; font-weight: bold; font-size: 10pt; text-align: center"> 
          <br /> 
          We are sorry, but an unhandled error occured on the server. The server administrator 
          has been notified and error logged. Please find error details below: 
          <br /> 
          <br /> 
         </td> 
        </tr> 
        <tr> 
         <td width="40%" align="center"> 
          <div id="ErrorInfo"> 
           <table> 
            <tr> 
             <td class="tdCol1Align"> 
              <b>Controller Name :</b> 
             </td> 
             <td class="tdCol2Align"> 
              <b>@Model.ControllerName</b> 
             </td> 
            </tr> 
            <tr> 
             <td class="tdCol1Align"> 
              <b>Action Name:</b> 
             </td> 
             <td class="tdCol2Align"> 
              <b>@Model.ActionName</b> 
             </td> 
            </tr> 
           </table> 
           <br /> 
           <br /> 
           <b>Error Description:</b><br /> 
           <br /> 
           <p> 
            @Model.Exception</p> 
          </div> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <br /> 
         </td> 
        </tr> 
       </table> 
      </div> 
     </div> 
     <div class="footer"> 
      <p> 
       xyz   </p> 
     </div> 
    </div> 
</body> 
</html> 

控制器错误查看:

public class ErrorController : Controller 
    { 
     /// <summary> 
     /// Action Method for Rendering Error View upon Error Occurance 
     /// </summary> 
     /// <returns></returns> 
     public ActionResult Error() 
     { 
      return View(); 
     } 
    } 
+0

我的确有类似的东西,但我也需要它来捕获IIS错误。 –

+0

@ BrendanVogt..IIS错误..任何示例错误?它捕获所有的错误 –