如何避免" Response.Redirect的不能在页回调&QUOT被调用; [英] How to avoid "Response.Redirect cannot be called in a Page callback"

查看:331
本文介绍了如何避免" Response.Redirect的不能在页回调&QUOT被调用;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我清理一些旧的框架code和巨大的量就可以简单地通过异常的编码。没有价值进行检查,看看他们是否为空,其结果是,异常丰富金额抛出和捕获。

我有但大多数人清理,有一些错误/登录/安全相关的框架,正在做Response.Redirect的方法,现在,我们正在使用AJAX,我们得到的的ALOT Response.Redirect的不能被称为一个页面的回调。我想,如果在所有可能避免这一点。

有没有一种方法以编程方式避免这种例外?我正在寻找类似

 如果(Request.CanRedirect)
    Request.Redirect(URL);

请注意,这也与Server.Transfer的发生,所​​以我希望能够检查,如果我能做到Request.Redirect或Server.Transfer的。

目前,它简单地做这个

 尝试
{
    Server.Transfer的(〜/ Error.aspx); //有时的Response.Redirect
}
赶上(例外ABC)
{
    //这里处理错误,该错误通常是:
    // Response.Redirect的不能在页回调调用
}


解决方案

您可以尝试

 如果(!Page.IsCallback)
    Request.Redirect(URL);

或者,如果你没有一个页面方便...

 尝试
{
    如果(HttpContext.Current == NULL)
    返回;
    如果(HttpContext.Current.CurrentHandler == NULL)
    返回;
    如果(!(HttpContext.Current.CurrentHandler是System.Web.UI.Page))
    返回;
    如果(((System.Web.UI.Page)HttpContext.Current.CurrentHandler).IsCallback)
    返回;    Server.Transfer的(〜/ Error.aspx);
}
赶上(例外ABC)
{
    // 处理它
}

I'm cleaning up some legacy framework code and a huge amount of it is simply coding by exception. No values are checked to see if they are null, and as a result, copious amounts of exceptions are thrown and caught.

I've got most of them cleaned up, however, There are a few error / login / security related framework methods that are doing Response.Redirect and now that we are using ajax, we are getting ALOT of "Response.Redirect cannot be called in a Page callback." And I'd like to avoid this if at all possible.

Is there a way to programatically avoid this exception? I'm looking for something like

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

Note, this is also happening with Server.Transfer, so I'd like to be able to check if I am able to do Request.Redirect OR Server.Transfer.

Currently, its simply doing this

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
}

解决方案

You can try

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

or if you dont have a Page handy...

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
}

这篇关于如何避免" Response.Redirect的不能在页回调&QUOT被调用;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆