gpt4 book ai didi

c# - 如果URL不包含现有 Controller ,则404永远不会达到Application_Error

转载 作者:行者123 更新时间:2023-12-03 07:58:55 25 4
gpt4 key购买 nike

我的global.asax中有以下代码:

protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
//...
}

如果我这样调用URL,则 NonExistingNonesense不存在:
localhost/ExistingArea/ExistingController/NonExistingNonesense

一切都很好。 (我的global.asax中的代码将被调用)

但是,如果我这样调用网址:
localhost/NonExistingNonesense

代码永远不会到达 Application_Error方法。

可能是什么问题呢?

我在web.config中有这个:
<system.web>
<customErrors mode="On"></customErrors>
<!-- ... -->
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
<!-- ... -->
<system.webServer>

请注意:

我知道有可能
<error statusCode="404" responseMode="ExecuteURL" path="/Error/PageNotFound" />

问题是,我需要将一些异常上下文传递给我的 View ,因此我想通过代码来处理它。

最佳答案

除非由IIS框架调用,否则您的ASP.NET代码不会触发。默认情况下,IIS将在到达代码之前停止对不存在的文件的任何请求。但是您可以关闭此功能。

进入IIS管理员并禁用Invoke handler only if request is mapped to a file选项。

关于c# - 如果URL不包含现有 Controller ,则404永远不会达到Application_Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com