有关通用重定向的System.Threading.ThreadAbortException

时间:2013-11-21 12:17:16

标签: c# asp.net .net generics redirect

我正在开展ASP.Net项目,我们有一个centralized redirection method。但有时它会引发异常:

  

System.Threading.ThreadAbortException

主要问题是在调用SBA.Redirect("AnotherPage.aspx")之后代码执行通常没有停止,并且以下代码仍在执行。

我的通用功能:

public static class SBA
{   
        public static void Redirect(string Url)
        {
                try
                {
                    HttpContext.Current.Response.Redirect(Url, false);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();                
                }
                catch (Exception ex)
                {
                    if (ex.GetType() != typeof(System.Threading.ThreadAbortException))
                    {
                        throw;
                    }
                }
        }
}

3 个答案:

答案 0 :(得分:0)

Redirect专门提出ThreadAbortException以阻止任何后续代码运行。

您正在处理ThreadAbortException

因此正在运行以下代码。

如果您不想运行以下代码,请不要处理ThreadAbortException

答案 1 :(得分:0)

只需进行以下调用即可进行重定向:

HttpContext.Current.Response.Redirect(Url);

您的代码存在两个问题:

  • 您使用Redirect的重载,通过为false参数提供endResponse,您决定结束响应。因此重定向后的代码执行。

  • 您尝试抓住ThreadAbortException。当使用如上所述的正常重定向时,抛出此异常。它不是一个错误条件,而只是ASP.NET确保正确终止当前请求的一种方法。你可以捕获异常,但它会在catch块的末尾重新抛出,所以你的catch块不会做任何有用的事情。

因为在重定向时抛出异常应该知道注释中解释的以下内容:

void HandleRequest() {
  try {
    Response.Redirect(" ... url ... ");
  }
  catch (Exception) {
    // Code here will execute after the redirect.
  }
}

为了避免出现问题,最好的方法是在catch处理程序中捕获更具体的异常类型,或者至少不在处理程序中做任何干扰重定向的事情(比如写入响应流)。

答案 2 :(得分:0)

我使用以下代码保护redirection。它正在发挥作用。

public static class SBA
{

    public static void Redirect(string Url)
    {
        try
        {
            //redirect only when 'IsRequestBeingRedirected' is false
            if (!HttpContext.Current.Response.IsRequestBeingRedirected)
            {
                Uri uri = null;
                bool isUriValid = Uri.TryCreate(Url, UriKind.RelativeOrAbsolute, out uri);

                if (!isUriValid)
                {
                    throw new SecurityException("Invalid uri " + Url);
                }

                //Below check is not required but checked
                //to make obsolate security check 
                if (uri.OriginalString == null)
                {
                    throw new SecurityException("Invalid uri " + Url);
                }

                // check if host is from configured trusted host list
                if (uri.IsAbsoluteUri)
                {
                    var tempAppSetting = ConfigBLL.GetAppSetting(AppSettingSectionType.OtherSetting).Other;
                    if (!tempAppSetting.RedirectTrustedUrls.Contains(uri.Host))
                    {
                        throw new SecurityException("Untrusted url redirection detected. Can not redirect.");
                    }
                }

                var tempUrl = uri.OriginalString;

                //Few more logical check 

                HttpContext.Current.Response.Redirect(tempUrl, true);
            }
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception ex)
        {
            if (ex.GetType() != typeof(System.Threading.ThreadAbortException))
            {
                throw;
            }
        }
    }
}