2016-05-13 95 views
3

我赶上未处理用我Global.asax文件中的Application_Error方法的错误,这是我到目前为止有:显示错误页面MVC

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception exception = Server.GetLastError(); 
     Response.Clear(); 

     HttpException httpException = exception as HttpException; 

     if(httpException != null) 
     { 
      string action; 

      switch(httpException.GetHttpCode()) 
      { 
       case 404: 
        action = "404"; 
        break; 
       case 500: 
        action = "500"; 
        break; 

       default: 
        action = "Other"; 
        break; 
      } 

       Server.ClearError(); 

       Response.Redirect(String.Format("~/Error/?error=" + action + "&message=" + exception.Message)); 
     } 
    } 

但是我真的不喜欢重定向用户的想法到错误页面,事实上我希望URL保持不变。

例如,当页面不存在时,它不应该重定向到URL中的路径,而应该保留在同一页面上,但仍显示错误。

有谁知道如何做到这一点?

回答

0

我的解决方案很像您的解决方案重定向,但取决于您的部署方式,我会通过在IIS中自定义错误页面来完成此操作。

Check Out this answered question

+0

这听起来很简单,但却很难。我所知道的只是在Application_Error渲染错误视图?而不是重定向 – KTOV