C# Response.Redirect 在 Global.asax 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14822435/
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 13:07:04  来源:igfitidea点击:

Response.Redirect not working in Global.asax

c#asp.netglobal-asaxresponse.redirect

提问by

I have created an error page to show a general message for all unhandled exceptions.

我创建了一个错误页面来显示所有未处理异常的一般消息。

This is the code in Global.asax

这是 Global.asax 中的代码

HttpContext ctx = HttpContext.Current;

            string e404_PAGE = ctx.Request.AppRelativeCurrentExecutionFilePath.ToString();
            string e404_LINE = ctx.Server.GetLastError().InnerException.StackTrace.Substring(ctx.Server.GetLastError().InnerException.StackTrace.LastIndexOf(":line ") + 6, ctx.Server.GetLastError().InnerException.StackTrace.Substring(ctx.Server.GetLastError().InnerException.StackTrace.LastIndexOf(":line ") + 6).IndexOf(" ")).ToString();
            string e404_MESSAGE = ctx.Server.GetLastError().InnerException.Message.ToString();
            string e404_METHODNAME = ctx.Server.GetLastError().InnerException.TargetSite.ToString();
            string e404_STACKTRACE = ctx.Server.GetLastError().InnerException.StackTrace.ToString();
            string e404_URL = ctx.Request.Url.ToString();
            string e404_DATE = ctx.Timestamp.ToString("yyyy-MM-dd HH:mm:ss");
            string e404_USER = ctx.User.Identity.Name.ToString();
            string e404_IP = ctx.Request.UserHostAddress.ToString();

            // * * * //

            System.Data.SqlClient.SqlConnection sql_conn;
            sql_conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
            sql_conn.Open();
            string query = @"insert into UnhandledExceptions(Message, Page, Line, MethodName, StackTrace, URL, Date, [User], IP) 
                                         values(@Message, @Page, @Line, @MethodName, @StackTrace, @URL, @Date, @User, @IP)
                                         select scope_identity();";
            System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(query, sql_conn);
            com.Parameters.AddWithValue("@Message", e404_MESSAGE);
            com.Parameters.AddWithValue("@Page", e404_PAGE);
            com.Parameters.AddWithValue("@Line", e404_LINE);
            com.Parameters.AddWithValue("@MethodName", e404_METHODNAME);
            com.Parameters.AddWithValue("@StackTrace", e404_STACKTRACE);
            com.Parameters.AddWithValue("@URL", e404_URL);
            com.Parameters.AddWithValue("@Date", e404_DATE);
            com.Parameters.AddWithValue("@User", e404_USER);
            com.Parameters.AddWithValue("@IP", e404_IP);

            string e404_ID = com.ExecuteScalar().ToString();

            sql_conn.Close();

            // * * * //            

            Session["e404_ID"] = e404_ID;
            Response.Redirect("~/Error.aspx");

When I publish the website, the user is not being redirected to the error page.

当我发布网站时,用户没有被重定向到错误页面。

All the code until the last line works fine. Any suggestion?

直到最后一行的所有代码都可以正常工作。有什么建议吗?

采纳答案by Sybeus

Replace Response.Redirect("~/Error.aspx");with:

替换Response.Redirect("~/Error.aspx");为:

// You've handled the error, so clear it. Leaving the server in an error state can cause unintended side effects as the server continues its attempts to handle the error.
Server.ClearError();

// Possible that a partially rendered page has already been written to response buffer before encountering error, so clear it.
Response.Clear();

// Finally redirect, transfer, or render a error view
Response.Redirect("~/Error.aspx");