Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 响应。重定向在Global.asax中不起作用_C#_Asp.net_Global Asax_Response.redirect - Fatal编程技术网

C# 响应。重定向在Global.asax中不起作用

C# 响应。重定向在Global.asax中不起作用,c#,asp.net,global-asax,response.redirect,C#,Asp.net,Global Asax,Response.redirect,我已经创建了一个错误页面来显示所有未处理异常的一般消息 这是Global.asax中的代码 HttpContext ctx = HttpContext.Current; string e404_PAGE = ctx.Request.AppRelativeCurrentExecutionFilePath.ToString(); string e404_LINE = ctx.Server.GetLastError().InnerException.S

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

这是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");
当我发布网站时,用户没有被重定向到错误页面


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

替换
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");

这个代码在你的Global.asax中的什么地方?@LuisTellez代码在应用程序中。\u error只是指出一个明显的问题:你已经声明
响应。重定向
不起作用。。但是您使用的是
Server.Transfer
。另外,您是否尝试过直接URL?(没有波浪号)@SimonWhitehead问题是我的应用程序中有文件夹和子文件夹,所以我需要Error.aspx页面的绝对路径,它就在根目录下。您的
web.config
中是否有
customErrors
属性?感谢您提供的解决方案和解释。嘿,我使用了上面相同的代码,但它仍然给出了错误响应,但在这种情况下不可用。这似乎解决了it记录错误,转到(并显然执行)重定向,但仍将默认错误页发送到浏览器(以及IIS日志中的500响应)。