System.Threading.ThreadAbortException上通用的重定向 [英] System.Threading.ThreadAbortException on generic redirection

查看:143
本文介绍了System.Threading.ThreadAbortException上通用的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个 asp.net 项目,我们有一个集中重定向方法 ..但有些时候它引发异常


  

System.Threading.ThreadAbortException


主要的问题是很多时候code执行不会停止呼吁后 SBA.Redirect(AnotherPage.aspx)和明年code是仍在执行。下面是我的通用功能.......

 公共静态类SBA
{
        公共静态无效重定向(字符串URL)
        {
                尝试
                {
                    HttpContext.Current.Response.Redirect(URL,FALSE);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                赶上(异常前)
                {
                    如果(ex.GetType()!= typeof运算(System.Threading.ThreadAbortException))
                    {
                        扔;
                    }
                }
        }
}


解决方案

我保护了重定向按低于code ..它的工作

 公共静态类SBA
{    公共静态无效重定向(字符串URL)
    {
        尝试
        {
            //重定向只有当'IsRequestBeingRedirected'是假的
            如果(!HttpContext.Current.Response.IsRequestBeingRedirected)
            {
                URI URI = NULL;
                布尔isUriValid = Uri.TryCreate(URL,UriKind.RelativeOrAbsolute,出URI);                如果(!isUriValid)
                {
                    抛出新SecurityException异常(无效的URI+网址);
                }                //下面检查是不是必需的,但检查
                //使obsolate安全检查
                如果(uri.OriginalString == NULL)
                {
                    抛出新SecurityException异常(无效的URI+网址);
                }                //检查主机是从配置的可信主机列表
                如果(uri.IsAbsoluteUri)
                {
                    VAR tempAppSetting = ConfigBLL.GetAppSetting(AppSettingSectionType.OtherSetting)。其它;
                    如果(!tempAppSetting.RedirectTrustedUrls.Contains(uri.Host))
                    {
                        抛出新SecurityException异常(不可信的URL重定向检测不能重定向。);
                    }
                }                VAR tempUrl = uri.OriginalString;                //很少有更多的逻辑检查                HttpContext.Current.Response.Redirect(tempUrl,真);
            }
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        赶上(异常前)
        {
            如果(ex.GetType()!= typeof运算(System.Threading.ThreadAbortException))
            {
                扔;
            }
        }
    }
}

I am working on a asp.net project where we have an centralized redirection method .. But some times it throwing exception

System.Threading.ThreadAbortException

The main problem is many times the code execution is not stopping after calling SBA.Redirect("AnotherPage.aspx") and next code are still executing. Below is my generic function.......

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;
                    }
                }
        }
}

解决方案

I protected the redirection by below code .. It's working

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;
            }
        }
    }
}

这篇关于System.Threading.ThreadAbortException上通用的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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