【问题标题】:Create Custom Error for Error 403.14 Directory Access Denied为错误 403.14 目录访问被拒绝创建自定义错误
【发布时间】:2018-01-16 16:36:54
【问题描述】:

这是我在这里的第一篇文章,我已经在这里和网络上的许多其他论坛上寻找解决此问题的方法,但没有成功。

我正在尝试为目录访问被拒绝错误 403.14 创建自定义错误,以防有人尝试在网站上加载“_assets”目录。我知道我可以通过将 default.aspx 页面添加到我希望发生这种情况的每个目录来解决问题,但想知道是否有类似于 web.config 文件中的标记的站点范围的解决方案

<configuration>
<system.web>
    <customErrors defaultRedirect="/Errors/GenericError.aspx" mode="RemoteOnly">
      <error statusCode="401"
         redirect="/Errors/401.aspx"/>
      <error statusCode="403"
         redirect="/Errors/403.aspx"/>
      <error statusCode="404"
         redirect="/Errors/404.aspx"/>
    <!-- 
      <error statusCode="403.14"
         redirect="/"/>
    -->
    </customErrors>
</system.web>
</configuration>

我在对 web.config 进行编码时收到错误,因为它不是 Int 数据类型,所以我无法使用带有小数的 statusCode

我在 Server 2008 上有一个 IIS 7。

有什么想法吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果这听起来相当混乱,请提前道歉。很高兴澄清。

    web.config 中添加了以下内容,到目前为止它似乎有效。不知道为什么,但如果我没有明确告诉 403 错误以重定向到自定义 403.aspx 页面,而不是 GenericError.aspx 页面,我会得到 500强>错误。但是,如果我将 404 错误重定向到我的自定义 404.aspx 页面,GenericError.aspx 代码会写在适当的位置,而不是像预期的那样,并且您似乎永远不会重定向到实际的 404.aspx 页面(见web.config 的评论部分)。奇怪。

    代码:

    web.config 文件:

    <system.webServer>
        <httpErrors existingResponse="Replace" errorMode="Custom">
            <remove statusCode="403"/>
            <remove statusCode="404"/>
            <error statusCode="403" path="/Errors/403.aspx" responseMode="Redirect"  />
    <!-- <error statusCode="403" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
    <!-- <error statusCode="404" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
    <error statusCode="404" path="/Errors/404.aspx" responseMode="Redirect"  />
        </httpErrors>
    </system.webServer>
    

    GenericError.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
    
        var ex = HttpContext.Current.Server.GetLastError();
    
        if (ex is HttpException)
        {
            var nex = ex as HttpException;
            //Label in the Main code displays the Error Code from the Error String
            this.customErrorMessageCode.Text += "Error Code" + " " + nex.GetHttpCode().ToString();
            //Label in the Main code displays the Error Message from the Error String
            this.customErrorMessageLabel.Text += ex.Message.ToString();
            //DIV ID in the Main code displays the entire error message
            this.customErrorMessage.Visible = true;
            switch (nex.GetHttpCode())
            {
                case 404:
                    this.customErrorMessageCode.Text += " Page Not Found";
                    this.customErrorMessageImage.Visible = true;
                    // do somehting cool
                    break;
                case 403:
                    this.customErrorMessageCode.Text += " Forbidden Access";
                    this.customErrorMessageImage.Visible = true;
                    // do somehting cool
                    break;
                case 500:
                    this.customErrorMessageCode.Text += " Internal Error";
                    this.customErrorMessageImage.Visible = true;
                    // do somehting cool
                    break;
                default:
                    break;
            }
        }
        else {
            this.customErrorMessageLabel.Text += ex.Message + ex.GetType().ToString();
        }
    
    }
    

    来源:

    CustomError in web.config

    【讨论】:

      【解决方案2】:

      也许您可以使用基于Server.GetLastError() 的实际错误代码的重定向方法。

      Global.asax 中你会有这样的东西:

      protected void Application_Error(object sender, EventArgs e)
      {
          if (Context.IsCustomErrorEnabled) {
              ShowCustomErrorPage(Server.GetLastError());
          }
      }
      

      ShowCustomErrorPage 然后会有一个 switch 语句读取 HTTP 代码并重定向到正确的错误页面。

      更多来自源链接,但它可能过于特定于 MVC。由于您没有提到您使用 MVC,我不想假设并盲目复制粘贴。

      我对 MVC 不太熟悉,但这里的原则看起来可以根据您的情况进行调整。

      来源:http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages

      编辑

      找到了一些 StackOverflow 帖子也可以提供帮助:

      Custom Error Handling in web.config / Global.asax not handling non-existant directory

      Custom error handling Asp.Net

      【讨论】:

      • 不,不使用 MVC。只是 asp.net Web 表单。
      猜你喜欢
      • 2014-01-31
      • 2015-08-24
      • 2011-05-21
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      相关资源
      最近更新 更多