asp.net - 静态文件请求不会引发application_error事件

标签 asp.net asp.net-mvc error-handling application-error

在我的Asp.Net MVC项目中,我正在处理Http FileNotFound Exceptions(针对丢失的图像),然后将请求重定向到如下所示的默认图像

  protected void Application_Error(object sender, EventArgs e)
    {
        if (Request.Path.StartsWith("/images/profile"))
        {

            Response.Redirect("/images/profile/default.jpg", true);
            return;
        }
    }

当我调试网站时,它正在开发环境中工作。但是,当我将其部署到运行IIS 7.5的生产服务器时,此代码不起作用,对图像文件的请求不会触发Application_Error事件。 IIS上有任何配置吗?我找不到问题。

最佳答案

您需要将IIS配置为通过ASP.Net运行所有请求。

<modules runAllManagedModulesForAllRequests="true" />添加到Web.config中的<system.webServer>中。

另外,您应该为此添加一条路由,而不是处理Error事件。

关于asp.net - 静态文件请求不会引发application_error事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671946/

相关文章:

html - 从 HTML 创建 PDF 时,IronPdf 会忽略 Bootstrap 列网格

c# - 缺少 DLL 和引用的问题

validation - Struts2转换警告

html - Web 应用程序分类 : appending some html to hide broken html

c# - 使用 Asp.Net Boilerplate 进行单元测试时获取新的 DbContext

c# - 如何在 git 存储库中保证 ASP.net 连接字符串密码的安全?

c# - 在网格中交互控件

javascript - asp.net标签刷新

asp.net - 您如何使用 ActionFilters 中断/拦截 MVC 操作?

validation - 在不重新加载页面的情况下在jsp中显示错误消息