V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zarte
V2EX  ›  .NET

.net 页面跳转报正在中止线程错误

  •  
  •   zarte · Jul 12, 2019 · 3794 views
    This topic created in 2493 days ago, the information mentioned may be changed or developed.

    判断用户是否登录,使用 HttpContext.Current.Response.Redirect 跳转到登录页面,会抛出异常。百度的结果是 Response.End 导致。
    解决办法是使用 Response.Redirect ( url,bool )方法但是!!会继续执行之后的代码,还有说加 HttpContext.Current.ApplicationInstance.CompleteRequest 的也一样会执行之后的代码。
    .net 就没有这种跳转需求吗?结束之后的代码不抛异常的?

    5 replies    2019-07-12 15:23:10 +08:00
    Rwing
        1
    Rwing  
       Jul 12, 2019
    return?
    arrow8899
        2
    arrow8899  
       Jul 12, 2019
    重定向是客户端做的事啊,Response.Redirect 只是给客户端返了个 302,服务端的逻辑可以继续执行,你不想继续执行就 return。
    zarte
        3
    zarte  
    OP
       Jul 12, 2019
    @arrow8899 问题是封装了个跳转的方法,return 后主方法之后的还是会执行。
    zarte
        4
    zarte  
    OP
       Jul 12, 2019
    @Rwing Page_Load 里调用别的方法,别的方法使用了跳转,return 就没用了。
    yulitian888
        5
    yulitian888  
       Jul 12, 2019   ❤️ 1
    因为没有结束请求的响应过程,加上 CompleteRequest 就好了
    我写的一个扩展方法,直接引用了就可以用 response 实例.RedirectTo("地址")来跳了

    public static class HttpRedirect
    {
    public static void RedirectTo(this HttpResponse response, string url)
    {
    if (response.IsRequestBeingRedirected)
    {
    return;
    }

    response.Redirect(url, false);
    var context = HttpContext.Current;
    if (context != null)
    {
    context.ApplicationInstance.CompleteRequest();
    }
    }
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3066 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 14:05 · PVG 22:05 · LAX 07:05 · JFK 10:05
    ♥ Do have faith in what you're doing.