如何使我的404错误调用我的自定义404方法?

时间:2018-03-19 18:06:04

标签: c# asp.net-mvc razor model-view-controller

我发现解决方案告诉我使用类似这样的东西(在system.web中)到make自定义错误页面:

<customErrors mode="On" defaultRedirect="~/home">
  <error redirect="~/home/page_not_found" statusCode="404" />
</customErrors>

和(在system.webServer中):

  <httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="404" path="Error404.html" responseMode="File" />
    <error statusCode="500" path="Error.html" responseMode="File" />
  </httpErrors>

我尝试了这两种方法,但似乎都没有。我不确定是否必须删除system.web和`system.webServer中的内容,或者我是否想要将路由发送到我希望显示错误消息的控制器in,所以我要告诉你我有什么

的Web.config:

<system.web>
  <compilation debug="true" targetFramework="4.5.2" />
  <httpRuntime targetFramework="4.5.2" />
  <httpModules>
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
  </httpModules>
</system.web>

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules>
    <remove name="ApplicationInsightsWebTracking" />
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
  </modules>
</system.webServer>

的web.config:

<system.web>
  <compilation>
    <assemblies>
      <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
  </compilation>
</system.web>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <handlers>
    <remove name="BlockViewHandler"/>
    <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  </handlers>
</system.webServer>

HomeController中:

public class HomeController : Controller
{
    ...
    [Route("~/page_not_found")]
    public ActionResult PageNotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
    [Route("~/internal_server_error")]
    public ActionResult InternalServerError()
    {
        Response.StatusCode = 500;
        return View();
    }
}

我的问题是:&#34;如何使用上述解决方案让我的程序重定向到错误页面?&#34;

方注意:如果你需要我的Route.Config,这里是:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();
}

2 个答案:

答案 0 :(得分:0)

不是404特定的,尽管如此:

<system.web>
  <customErrors mode="On" defaultRedirect="~/Error"></customErrors>

您可以将上方的“开”更改为“关闭”。

要在控制器操作中进行测试:

throw new Exception();

在视图中,共享,创建Error.cshtml

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h1 class="text-danger">Error.Custom.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

答案 1 :(得分:0)

webconfig中的<system.webServer>添加

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

之后创建ErrorController并添加以下操作:

public ActionResult Error404()
 {
    return View();
 }

之后在Errors文件夹中创建自定义Error404.cshtml视图