C# 如何替换“错误。处理您的请求时出错。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17913523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:47:27  来源:igfitidea点击:

How to replace "Error. An error occurred while processing your request."

c#.netasp.net-mvcasp.net-mvc-4

提问by Andrzej Gis

In my Web.config I have.

在我的 Web.config 我有。

<customErrors mode="On" defaultRedirect="~/Errors/Unknown">
  <error statusCode="403" redirect="~/Errors/Forbidden"></error>
  <error statusCode="404" redirect="~/Errors/NotFound"></error>
</customErrors>

It works fine if I try to open a page that doesn't exist. (It redirects to my custom error page: ErrorsController.NotFound). When an unhandled exception occurs (in this case in LINQ Signle(...)). It doesn't go to ~/Errors/Unknown, but displays default message:

如果我尝试打开一个不存在的页面,它工作正常。(它重定向到我的自定义错误页面:ErrorsController.NotFound)。发生未处理的异常时(在本例中为 LINQ Signle(...))。它不会转到 ~/Errors/Unknown,而是显示默认消息:

Error. An error occurred while processing your request.

错误。处理您的请求时发生错误。

How replace it with my ErrorsController.Unknown ?

如何用我的 ErrorsController.Unknown 替换它?

采纳答案by Andrzej Gis

Thanks to this questionI figured out how to do this. The code below is almost the same as the accepted answer's. The modification handles no HttpExceptions too.

由于这个问题,我想出了如何做到这一点。下面的代码与接受的答案几乎相同。修改也不处理 HttpExceptions。

Steps to make it work:

使其工作的步骤:

  1. customErrors element in web.config will now be ignored
  2. Paste the method below to your Global.axax.

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Response.Clear();
    
        var httpException = exception as HttpException;
    
        if (httpException != null)
        {
            string action;
    
            switch (httpException.GetHttpCode())
            {
                case 404:
                    // page not found
                    action = "NotFound";
                    break;
                case 403:
                    // forbidden
                    action = "Forbidden";
                    break;
                case 500:
                    // server error
                    action = "HttpError500";
                    break;
                default:
                    action = "Unknown";
                    break;
            }
    
            // clear error on server
            Server.ClearError();
    
            Response.Redirect(String.Format("~/Errors/{0}", action));
        }else
        {
            // this is my modification, which handles any type of an exception.
            Response.Redirect(String.Format("~/Errors/Unknown")); 
        }
    }
    
  1. web.config 中的 customErrors 元素现在将被忽略
  2. 将以下方法粘贴到您的 Global.axax。

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Response.Clear();
    
        var httpException = exception as HttpException;
    
        if (httpException != null)
        {
            string action;
    
            switch (httpException.GetHttpCode())
            {
                case 404:
                    // page not found
                    action = "NotFound";
                    break;
                case 403:
                    // forbidden
                    action = "Forbidden";
                    break;
                case 500:
                    // server error
                    action = "HttpError500";
                    break;
                default:
                    action = "Unknown";
                    break;
            }
    
            // clear error on server
            Server.ClearError();
    
            Response.Redirect(String.Format("~/Errors/{0}", action));
        }else
        {
            // this is my modification, which handles any type of an exception.
            Response.Redirect(String.Format("~/Errors/Unknown")); 
        }
    }