【问题标题】:ASP.NET MVC 5 - Error Handle for - 404 Page not foundASP.NET MVC 5 - 错误句柄 - 404 页面未找到
【发布时间】:2019-02-19 07:28:05
【问题描述】:

对于标准错误,我使用解决方案错误句柄(参见下面的代码),并且我尝试了THIS(无效)

Global.asax.cs

 protected void Application_Error(object sender, EventArgs e)
 {
        var exception = Server.GetLastError();

        var httpException = exception as HttpException;
        var routeData = new RouteData();

        routeData.Values.Add("controller", "Error");

        if (httpException == null)
            routeData.Values.Add("action", "Index");
        else //It's an Http Exception, Let's handle it.
            switch (httpException.GetHttpCode())
            {
                case 404:
                    // Page not found.
                    routeData.Values.Add("action", "HttpError404");
                    break;
                case 500:
                    // Server error.
                    routeData.Values.Add("action", "HttpError500");
                    break;

                // Here you can handle Views to other error codes.
                // I choose a General error template  
                default:
                    routeData.Values.Add("action", "General");
                    break;
            }

        // Pass exception details to the target error View.
        routeData.Values.Add("error", exception);

        var request = Request;
        // Pass request details to the target request View.
        routeData.Values.Add("request", request);

        // Clear the error on server.
        Server.ClearError();

        // Avoid IIS7 getting in the middle
        Response.TrySkipIisCustomErrors = true;

        // Call target Controller and pass the routeData.
        IController errorController = new ErrorController();
        errorController.Execute(new RequestContext(
            new HttpContextWrapper(Context), routeData));
}

错误控制器

public class ErrorController : Controller
{
    private readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    // GET: Error
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult HttpError404(string error, HttpRequest request)
    {
        ViewBag.Title = "Page not found (Error 404)";
        ViewBag.Description = error;
        return View("~/Views/Shared/Error.cshtml");
    }

    public ActionResult HttpError500(string error, HttpRequest request)
    {
        ViewBag.Title = "Internal server error (Error 500)";
        ViewBag.Description = error;
        return View("~/Views/Shared/Error.cshtml");
    }

    public ActionResult General(string error, HttpRequest request)
    {
        ViewBag.Title = "Internal server error";

        ViewBag.Description = error;
        return View("~/Views/Shared/Error.cshtml");
    }
}

Error.cshtml

@{
    Layout = "_Layout.cshtml";
}

<div class="row">
    <div class="exceptionmessage col-md-12 text-center">
        <div class="center-block">
            <img src="~/Content/i/404.png" class="img-responsive"/>
        </div>

        <strong>@ViewBag.Message</strong>
        <span class="center-block small text-uppercase">@ViewBag.Main</span>


        <div class="center-block">@Html.ActionLink("Return to the Homepage", "Index", "Home", null, new {@class = "btn btn-primary"})</div>
    </div>
</div>

我有一个目录Techs,其中有一些 XML 文件(用于加载数据,例如本地 DB),并且使用 web.config 中的此脚本,我禁用对此文件夹的访问并从此文件夹下载文件(或当我写不存在的 URL 或类似文件的 www.test.com/afsdfgsdfg.pdf) 时,我收到 IIS 消息 404 Page not found 没有从下面的错误处理程序代码重定向到我的自定义错误 404 页面。

  <system.web>    
    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/HttpError404" statusCode="404" />
    </customErrors>
  </system.web>

  <system.webServer>    
    <security xdt:Transform="Replace">
      <requestFiltering>
        <hiddenSegments>
          <add segment="Techs" />
        </hiddenSegments>
      </requestFiltering>
    </security>
  </system.webServer>

有什么方法可以处理所有找不到 404 页面的错误(相同的页面而不是两个不同的页面)?

谢谢

编辑:

我将我的ErrorController 的新路由添加到RouteConfig.cs

routes.MapRoute("Error",
                "{controller}/{action}/",
                new { controller = "Error", action = "HttpError404" });

之后我将web.config 更改为:

<customErrors mode="On" defaultRedirect="~/Error/General">
    <error redirect="~/Error/HttpError404" statusCode="404" />
    <error redirect="~/Error/HttpError500" statusCode="500" />
</customErrors>

<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" />
      <error
        statusCode="404"
        path="/Error/HttpError404"
        responseMode="ExecuteURL"/>
    </httpErrors>

但它仍然不起作用,我收到 HTTP Error 404.0 - Not FoundHTTP Error 403.14 - Forbidden 和类似的消息,例如当我编写时: p>

  • web.com/Techs/
  • web.com/Techs/catalog.csv
  • web.com/Techs.pdf -> 重定向到 404 页面
  • web.com/Home/c.pdf

【问题讨论】:

  • 你有没有试过在路由注册中最后添加默认路由?就像如果任何路线不匹配然后重定向到您的 404 操作
  • 嗨,错误句柄中没有我的 RouteConfig.cs 中没有任何路由

标签: c# iis error-handling asp.net-mvc-5


【解决方案1】:

我的可行解决方案

我没有向 RouteConfig.cs 添加任何新路由,只编辑下面的文件。我在没有参数的情况下调用方法的控制器,我受到了不同的对待。 Global.asax.cs 和 ErrorController 使用同上。

Web.config

<system.web>   
    <customErrors mode="On" defaultRedirect="~/Error/General">
      <error redirect="~/Error/HttpError404" statusCode="403" />
      <error redirect="~/Error/HttpError404" statusCode="404" />
      <error redirect="~/Error/HttpError500" statusCode="500" />
    </customErrors>

  </system.web>

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="403" />
      <remove statusCode="404" />
      <remove statusCode="500" />
      <error
        statusCode="403"
        path="/Error/HttpError404"
        responseMode="ExecuteURL" />
      <error
        statusCode="404"
        path="/Error/HttpError404"
        responseMode="ExecuteURL" />
      <error
        statusCode="500"
        path="/Error/HttpError500"
        responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>

【讨论】:

    【解决方案2】:

    web.config 中添加这一行

    <system.web>
        <customErrors mode="On" defaultRedirect="~/ErrorHandler/Index">
            <error statusCode="404" redirect="~/ErrorHandler/NotFound"/>
        </customErrors>
    <system.web/>
    

    编辑: 这应该涵盖该代码的所有错误......但我看到你已经有了。

    【讨论】:

      【解决方案3】:

      您可以在主目录中创建一个名为 Error 的文件夹,然后使用您自己的设计创建一个 html 错误页面。

      在您的网络配置中,添加以下内容:

      <customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
        <error statusCode="404" redirect="/Error/404.html" />
      </customErrors>
      

      这将重定向到错误文件夹中的 404.html

      如果您想检查控制器中的特定错误并重定向到相关的错误页面,您可以在控制器中创建一个 ErrorController 以及要检查的不同类型错误的视图在 Error 视图文件夹中。例如:

          //In the ErrorController
          // GET: /Error/404 Bad request
          public ActionResult E404()
          {
              return View();
          }
      

      如果找不到页面或 id == null 在控制器中,您可以执行以下操作:

      if (id == null)
      {
          return RedirectToAction("E404", "Error");
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-22
        • 2012-09-15
        • 2021-09-06
        • 2015-03-30
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 2012-11-11
        相关资源
        最近更新 更多