简体   繁体   English

在web.config中使用httpErrors时未命中错误控制器

[英]Error controller not hit when using httpErrors in web.config

I am struggling to setup the httpErrors section correctly in my web.config to capture both ASP.NET MVC errors and IIS errors . 我正在努力在我的web.config正确设置httpErrors部分以捕获ASP.NET MVC errorsIIS errors I am getting 403 status codes and blank pages. 我收到403状态代码和空白页。 I am testing with 404 errors by typing incorrect URLS and file names in the URL, for example: 我通过在URL中键入错误的URL和文件名来测试404错误,例如:

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

I am using the latest version of ASP.NET MVC 5 . 我正在使用最新版本的ASP.NET MVC 5 This web application is running on IIS 7.5 with an application pool using integrated mode . 该Web应用程序在IIS 7.5上运行,并带有使用integrated mode的应用程序池。

This is what my web.config looks like at the root of the application (this is all that I currently have): 这是我的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>

My global.asax.cs file: 我的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
        }
    }
}

My error controller: 我的错误控制器:

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();
    }
}

They have their corresponding views in the Views folder. 它们在“ Views文件夹中具有相应的Views

My break points are never hit in my error controller. 我的断点从未在我的错误控制器中命中。 I don't understand why? 我不明白为什么? I have looked at many examples on Stackoverflow and this is how every suggests that I do it. 我看了很多关于Stackoverflow的例子,这就是每个人都建议我这样做的方式。 When would the break points not be reached given my code? 给定我的代码,什么时候不能达到断点? All that happens is a 403 error status and a BLANK white page. 所有发生的是403错误状态和空白页面。 The break points in Application_Error() and Application_EndRequest() are hit, but not the break points in the error controller. 命中了Application_Error()Application_EndRequest()中的断点,但未命中错误控制器中的断点。

There is code that I can write in Application_Error() and Application_EndRequest() that lets me set the error controller and action method, but why do that if I can use the web.config? 我可以在Application_Error()Application_EndRequest()中编写一些代码,这些代码可以让我设置错误控制器和操作方法,但是如果可以使用web.config,为什么要这样做呢? This should also work? 这还应该工作吗?

Man, you have mentioned statusCode as 404 then how can you expect as 200 OK ? 伙计,您提到过statusCode为404,那么怎么会期望为200 OK? :) :)

just remove Response.StatusCode = 404; 只需删除Response.StatusCode = 404; line from action method 行动法

Bellow code should work 波纹管代码应该工作

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

检查此图像。工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM