【问题标题】:How to avoid "Response.Redirect cannot be called in a Page callback"如何避免“无法在页面回调中调用 Response.Redirect”
【发布时间】:2010-12-05 01:20:28
【问题描述】:

我正在清理一些遗留框架代码,其中大量代码只是通过异常进行编码。不检查值是否为空,因此会引发和捕获大量异常。

我已经清理了大部分,但是,有一些错误/登录/安全相关的框架方法正在执行 Response.Redirect,现在我们正在使用 ajax,我们得到了很多 " Response.Redirect 不能在页面回调中调用。”如果可能的话,我想避免这种情况。

有没有办法以编程方式避免此异常?我正在寻找类似的东西

if (Request.CanRedirect)
    Request.Redirect("url");

请注意,Server.Transfer 也会发生这种情况,因此我希望能够检查我是否能够执行 Request.Redirect OR Server.Transfer。

目前,它只是这样做

try
{
    Server.Transfer("~/Error.aspx"); // sometimes response.redirect
}
catch (Exception abc)
{
    // handle error here, the error is typically:
    //    Response.Redirect cannot be called in a Page callback
}

【问题讨论】:

    标签: c# asp.net asp.net-ajax exception-handling response.redirect


    【解决方案1】:

    你可以试试

    if (!Page.IsCallback)
        Request.Redirect("url");
    

    或者如果您手边没有页面...

    try
    {
        if (HttpContext.Current == null)
            return;
        if (HttpContext.Current.CurrentHandler == null)
            return;
        if (!(HttpContext.Current.CurrentHandler is System.Web.UI.Page))
            return;
        if (((System.Web.UI.Page)HttpContext.Current.CurrentHandler).IsCallback)
            return;
    
        Server.Transfer("~/Error.aspx");
    }
    catch (Exception abc)
    {
        // handle it
    }
    

    【讨论】:

    • 在 HttpContext.Current 中哪里可以找到 IsCallback?我不会总是有一个页面
    • 啊,找到了,我将最终使用的代码添加到您的答案中,因为您的 sn-p 确实是我所需要的。我不敢相信我自己没有想到这么简单的事情,呵呵。谢谢大佬
    【解决方案2】:

    我相信您可以简单地将Server.Transfer() 替换为Response.RedirectLocation(),这在回调期间有效。

    try
    {
        Response.RedirectLocation("~/Error.aspx"); // sometimes response.redirect
    }
    catch (Exception abc)
    {
        // handle error here, the error is typically:
        //    Response.Redirect cannot be called in a Page callback
    }
    

    【讨论】:

    • 作为记录,我发现 (ASP.NET 4.x) a) Response.RedirectLocation 是一个属性而不是一个方法,并且 b) 它没有扩展 ~ 符号,所以你需要Response.RedirectLocation = Page.ResolveUrl("~/Error.aspx")
    【解决方案3】:

    如上所述,但扩展为包括 .NET 4.x 版本,并在没有可用的Page 时分配给Response.RedirectLocation 属性。

    try 
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
    catch (ApplicationException) 
    {
        HttpContext.Current.Response.RedirectLocation =    
                             System.Web.VirtualPathUtility.ToAbsolute("~/Error.aspx");
    }
    

    【讨论】:

    • 确实有效。非常感谢,你解决了我的大问题
    【解决方案4】:

    您应该加载您的 ScriptManager 或 ScriptManagerProxy,然后检查 IsInAsyncPostBack 标志。看起来像这样:

    ScriptManager sm = this.Page.Form.FindControl("myScriptManager") as ScriptManager;
    if(!sm.IsInAsyncPostBack)
    {
        ...
    }
    

    通过这样做,您可以将异步回发(应该无法重定向)与正常回发混合使用,我假设您仍想重定向。

    【讨论】:

    • hrm,你能详细说明一下这和 Page.IsCallback 有什么区别吗?
    • 看起来不同之处在于 IsCallback 适用于 Asp.net ICallbackEventHandler 接口,而 IsInAsyncPostBack 适用于基于 UpdatePanel 的更改。这是我能找到的最直接的比较:timstall.dotnetdevelopersjournal.com/…
    • 也可以使用ScriptManager.GetCurrent(Page);
    猜你喜欢
    • 2017-05-19
    • 2013-09-17
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多