我的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/