C# 如何避免“无法在页面回调中调用 Response.Redirect”

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

How to avoid "Response.Redirect cannot be called in a Page callback"

c#asp.netasp.net-ajaxexception-handlingresponse.redirect

提问by Allen Rice

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.

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

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.

请注意,Server.Transfer 也会发生这种情况,因此我希望能够检查我是否能够执行 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
}

采纳答案by Brandon

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
}

回答by Dan Monego

You should load up your ScriptManager or ScriptManagerProxy, and then check the IsInAsyncPostBack flag. That would look something like this:

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

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

By doing this, you can mix async postbacks (which should fail to redirect) with normal postbacks, which I'm assuming you still want to redirect.

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

回答by Fredrik Eder

I believe you can simply replace Server.Transfer()with Response.RedirectLocation()which works during callback.

我相信您可以简单地替换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
}

回答by WheretheresaWill

As mentioned above, but expanded to include the .NET 4.x versionand assigning to the Response.RedirectLocationproperty when there is no Pageavailable.

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

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