Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 5-找不到-404页的错误句柄_C#_Iis_Error Handling_Asp.net Mvc 5 - Fatal编程技术网

C# ASP.NET MVC 5-找不到-404页的错误句柄

C# ASP.NET MVC 5-找不到-404页的错误句柄,c#,iis,error-handling,asp.net-mvc-5,C#,Iis,Error Handling,Asp.net Mvc 5,对于标准错误,我使用解决方案错误句柄(见下面的代码),并且我也尝试过(没有效果) Global.asax.cs protected void Application_Error(object sender, EventArgs e) { var exception = Server.GetLastError(); var httpException = exception as HttpException; var routeData = n

对于标准错误,我使用解决方案错误句柄(见下面的代码),并且我也尝试过(没有效果)

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");
    }
}
错误。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>
有没有办法处理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错误404.0-未找到HTTP错误403.14-禁止和类似消息,例如:

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

web.config中添加此行

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

编辑:
这应该涵盖了代码中的所有错误……但我发现您已经有了这些错误。

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

在web配置中,添加以下内容:

<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="/Error/404.html" />
</customErrors>
在控制器中,如果找不到页面或id==null,则可以执行以下操作:

if (id == null)
{
    return RedirectToAction("E404", "Error");
}
我的解决方案有效

我不向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>


您是否尝试过在路线注册中最后添加默认路线?如果任何路由不匹配,则重定向到404 actionHi,no from Error handle我的RouteConfig.cs中没有任何路由
if (id == null)
{
    return RedirectToAction("E404", "Error");
}
<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>