【问题标题】:Resonse.Redirect(URL) is not working in global.asaxResponse.Redirect(URL) 在 global.asax 中不起作用
【发布时间】:2018-01-09 09:07:11
【问题描述】:

在我的要求中,我返回了自定义错误。 global.asax 应用程序错误未重定向任何其他 URL。 Response.Redirect(URL),Server.Transfer(URL) 没有重定向。它显示发送 HTTP 标头后无法重定向。我试过但没有工作。请尝试帮助我。下面是我的代码

try
        {
            Exception exc = Server.GetLastError();
            ErrorLogger LogError = new ErrorLogger();

            // Handle HTTP errors
            if (exc.GetType() == typeof(HttpException))
            {
                ex = (HttpException)Server.GetLastError();
                int httpCode = ex.GetHttpCode();

                // Filter for Error Codes and set text
                if (httpCode >= 400 && httpCode < 500)
                    ex = new HttpException
                          (httpCode, "Safe message for " + httpCode + " HTTP codes.", ex);
                else if (httpCode > 499)
                    ex = new HttpException
                         (ex.ErrorCode, "Safe message for " + httpCode + " HTTP codes.", ex);
                else
                    ex = new HttpException
                        (httpCode, "Safe message for unexpected HTTP codes.", ex);

                // Log the exception and notify system operators
                ErrorLogger.Error(ex);
                Server.ClearError();
                Response.Clear();
                Response.Buffer = true;
                Response.Redirect("http://www.stackoverflow.com");
                if (!Response.IsRequestBeingRedirected)
                    // Will not be called
                    Response.Redirect("http://www.google.com");

                //ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('ErrorPage.aspx');", true);
                //Response.Write("<script> window.open('http://localhost:54749/ErrorPage.aspx','_blank'); </script>");
                //Response.Redirect("http://www.google.com", true);
                //Response.Redirect("~/ErrorPage.aspx");
            }
            else
            {
                ErrorLogger.Error(exc);
                // LogError.NotifySystemOps(exc);
                // Clear the error from the server
                //Server.ClearError();
            }
        }
        catch (Exception ex)
        {
            ErrorLogger.Error(ex);
        }

【问题讨论】:

标签: c# asp.net asp.net-mvc-4 global-asax


【解决方案1】:

根据Response.Redirect(string url) 的 MSDN 文档,当“在发送 HTTP 标头后尝试重定向”时,它将抛出 HttpException。由于Response.Redirect(string url) 使用Http“Location”响应标头(http://en.wikipedia.org/wiki/HTTP_headers#Responses),调用它会导致标头被发送到客户端。这意味着如果您第二次调用它,或者如果您在以其他方式发送标头后调用它,您将获得 HttpException。

防止多次调用Response.Redirect() 的一种方法是在调用Response.IsRequestBeingRedirected property (bool) 之前对其进行检查。

// 导致将标头发送到客户端 (Http "Location" response header)

Response.Redirect("http://www.stackoverflow.com");

if (!Response.IsRequestBeingRedirected)
    // Will not be called
    Response.Redirect("http://www.google.com");

注意* 一旦发出重定向 301/302,您就无法更改 HTTP 响应状态代码。

【讨论】:

  • 我已经试过了。请查看我已经实现的代码
【解决方案2】:

你试过 Response.Redirect("http://www.google.com", true);

您能否告诉我们您正在使用此代码的 global.asax 文件的哪个部分。

void Application_BeginRequest(object sender, EventArgs e)
    {
}

或者在别的地方

也可以试试下面的代码

HttpContext.Current.Response.Redirect("http://www.google.com/");

【讨论】:

  • 我已经试过你的答案了。我的代码就是这个方法。 protected void Application_Error(Object sender, EventArgs e) { }
【解决方案3】:

试试这个:

this.Response.Redirect("http://www.stackoverflow.com");

【讨论】:

  • 同样的错误,即:发送 HTTP 标头后无法重定向。
  • 如果您使用的是 ASP.NET MVC,那么对于错误处理,请使用过滤器 (OnExecption) 而不是 global.asax
  • 没有。我正在使用 webapi
  • Form WebAPI 同样,您可以管理过滤器并发送带有适当状态代码和消息的响应。在调用 API 的 Web 端检查状态代码并重定向到错误页面
  • @chaitanay:当这是一个 webapi 项目时,Response.Redirect 是什么意思?
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多