【问题标题】:why Application_Error can not handlle some errors?为什么 Application_Error 不能处理一些错误?
【发布时间】:2015-02-28 08:00:39
【问题描述】:

我开发了一个网站(MVC 5)并将其上传到网络服务器上的 iis7 上。 我处理方法上的错误

protected void Application_Error(){}

在 Global.asax 上。 昨天我做了一些测试,当我输入这个网址时我看到了

http://www.xxxx.com/.

我可以在 Application_Error 方法上获得无效的路由,但是当我输入带有 3dotes 或更多类似此 URL 的 URL 时

http://www.xxxx.com/...

我看到一个包含此内容的网页,但 Application_Error 不起作用,因为我的项目中没有包含此内容的默认页面或视图。

The page cannot be displayed because an internal server error has occurred.

我在互联网上的另一个网站上做了一些测试,我看到一些主题有同样的问题,我想我应该在我的 IIS 上做一些配置。

如果是,我应该在我的 IIS 上设置什么配置,如果没有,我可以用我的项目做什么,直到我可以处理它?

【问题讨论】:

    标签: asp.net-mvc-4 iis-7


    【解决方案1】:

    您可以在 web.config 文件中尝试此配置:

       <customErrors mode="On|Off|RemoteOnly" defaultRedirect="URL">
          <error statusCode="404" redirect="URL" />
          <error statusCode="402" redirect="URL" />
          <error statusCode="500" redirect="URL" />
       </customErrors>
    

    示例
    我使用以下 ActionResult 创建了一个 ErrorHandlerController

    //This action return a view that show the http error you specified.
    public ActionResult Error()
    {
        return View();
    }
    
    //This action return a view that show the 404 http error captured.
    public ActionResult NotFoud()
    {
        return View();
    }
    
    //This action return a view that show the 402 http error captured.
    public ActionResult Unauthorized()
    {
        return View();
    }
    


    在我的项目的 web.config 文件中:

    <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorHandler/Error">
      <error statusCode="404" redirect="~/ErrorHandler/NotFoud" />
      <error statusCode="402" redirect="~/ErrorHandler/Unauthorized" />
      <error statusCode="500" redirect="~/ErrorHandler/Error" />
    </customErrors>
    

    注意:将 URL 更改为所需的 CustomURL。

    帮助:https://msdn.microsoft.com/en-us/library/h0hfz6fc%28v=vs.85%29.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多