【问题标题】:Error controller not hit when using httpErrors in web.config在 web.config 中使用 httpErrors 时错误控制器未命中
【发布时间】:2014-11-18 11:47:38
【问题描述】:

我正在努力在我的web.config 中正确设置httpErrors 部分以捕获ASP.NET MVC errorsIIS errors。我收到 403 状态代码和空白页。我通过在 URL 中输入不正确的 URL 和文件名来测试 404 错误,例如:

www.mywebsite.com/test
www.mywebsite.com/test.html

我正在使用最新版本的ASP.NET MVC 5。此 Web 应用程序在 IIS 7.5 上运行,应用程序池使用 integrated mode

这就是我的web.config 在应用程序根目录下的样子(这是我目前拥有的全部):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0"/>
        <add key="webpages:Enabled" value="false"/>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>

    <system.web>
        <customErrors mode="Off" />
        <compilation debug="true" targetFramework="4.5.2"/>
        <httpRuntime targetFramework="4.5.2"/>
    </system.web>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <httpErrors errorMode="Custom" existingResponse="Replace">
            <remove statusCode="404" />
            <remove statusCode="500" />
            <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
            <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
        </httpErrors>
    </system.webServer>

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
                <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
            </dependentAssembly>
            <!-- ...and so forth... -->
        </assemblyBinding>
    </runtime>
</configuration>

我的global.asax.cs 文件:

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_Error()
    {
        // Break point has been set here for testing purposes
    }

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            // Break point has been set here for testing purposes
        }
    }
}

我的错误控制器:

public class ErrorController : Controller
{
    public ActionResult Index()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 500;

        return View();
    }

    public ActionResult Forbidden()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 403;

        return View();
    }

    public ActionResult NotFound()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 404;

        return View();
    }
}

它们在Views文件夹中有对应的视图。

我的错误控制器中永远不会遇到我的断点。我不明白为什么?我在 Stackoverflow 上查看了许多示例,这就是每个人都建议我这样做的方式。给定我的代码,何时无法达到断点?所发生的只是 403 错误状态和空白白页。 Application_Error()Application_EndRequest() 中的断点被命中,但错误控制器中的断点未命中。

我可以在Application_Error()Application_EndRequest() 中编写代码,让我设置错误控制器和操作方法,但如果我可以使用web.config,为什么要这样做?这也应该有效?

【问题讨论】:

  • 你能检查一下提琴手中的 URL 流吗,我在我的示例应用程序中实现了同样的东西,对我来说它在 404 上工作正常,请确保你没有实现任何异常过滤器。
  • Fiddler 中的状态码也显示 404,我没有添加过滤器。我在哪里检查这个?我为此创建了一个新的空 MVC Web 应用程序。如果我将其更改为显示错误的 html 静态页面,那么它 100% 正确。当我使用控制器时,它显示为空白。
  • @BrendanVogt 我遇到了完全相同的问题。关键的相似之处是触发 404 ExecuteURL 时带有空白页面的 403 Forbidden HTTP 响应。你有想过这个吗?

标签: c# asp.net asp.net-mvc asp.net-mvc-4 iis


【解决方案1】:

伙计,你提到 statusCode 为 404 那么你怎么能期望 200 OK 呢? :)

只需从操作方法中删除 Response.StatusCode = 404;

下面的代码应该可以工作

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

【讨论】:

  • 此时无所谓,因为它根本没有进入这个控制器。我在这里设置了一个未达到的断点。我不知道为什么。
  • 检查我在上图中的代码,这正是我所做的,它都为我工作。那一定和你的路线有冲突,你能给我看看你的 routeConfig.cs 文件吗?
  • 请将您的示例应用程序发给我好吗?您使用的是什么版本的 IIS? ASP.NET MVC 的版本?
  • .NET Framework 4.5.1,MVC 5, IIS 7.5.7600.16385,您可以分享您的示例应用程序,我将针对该问题进行验证
  • 我在 IIS 7.5 下运行我的应用程序。你不是,你正在使用 IIS Express。我创建了一个新项目并像你一样运行它,它运行良好。使用本地 IIS 时似乎是 IIS 问题/权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 2013-12-05
  • 1970-01-01
  • 2011-05-20
  • 2016-08-20
  • 1970-01-01
相关资源
最近更新 更多