简体   繁体   English

由于我在 Web.config 和 Global.asax 中的设置,NotFound 方法被调用两次

[英]A NotFound method is invoked twice due to my settings in Web.config and Global.asax

I have an MVC5 application.我有一个 MVC5 应用程序。 In the config file, I have set the following:在配置文件中,我设置了以下内容:

    <httpErrors errorMode="Custom" existingResponse="Replace">
      <clear/>
      <error statusCode="400" responseMode="ExecuteURL" path="/Error/BadRequest" />
      <error statusCode="403" responseMode="ExecuteURL" path="/Error/Forbidden" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFoundFromWebConfig" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Error/Index" />
    </httpErrors>

I also in the Global.asax.cs file in the Application_Error method, handle the error:我也在Global.asax.cs文件中的Application_Error方法中,处理错误:

     Response.TrySkipIisCustomErrors = true;
     HttpContext.Current?.Response.Redirect("~/Error/NotFound", true);

Now, when I hit a controller for example mysite/contactus/blah, I see that the NotFound method is invoked twice.现在,当我点击控制器(例如 mysite/contactus/blah)时,我看到 NotFound 方法被调用了两次。

I have tried to disable one of these two but I get different issues when I do that.我试图禁用这两个中的一个,但是当我这样做时我遇到了不同的问题。 For example if I remove the one in the config file, when 404 happens, I get that ugly black and red page.例如,如果我删除配置文件中的那个,当 404 发生时,我会看到丑陋的黑色和红色页面。 If I remove the second one, I get redirected to a blank page.如果我删除第二个,我会被重定向到一个空白页面。

Can someone please help?有人可以帮忙吗? Thank you.谢谢你。

As per conversation above, I removed the error handling from the Global.asax andI also had a catch all route in my routeconfig file which I removed since I already restrict my routes by a condition.根据上面的对话,我从 Global.asax 中删除了错误处理,并且我的 routeconfig 文件中还有一个 catch all 路由,因为我已经通过条件限制了我的路由,所以我将其删除。 This fixed the issue.这解决了这个问题。

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

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