日期:2014-05-17  浏览次数:20465 次

线程中调用Request报"请求在此上下文中不可用。"
C# code

public string DecryptionParms
    {
        get
        {
            return Request.QueryString["tosee"];  //这里报错
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (CheckParms())
            {
                Thread thread = new Thread(Check);

                thread.IsBackground = true;

                thread.Start();
                
                Response.Write(" ");
            }
        }
    }

    protected static readonly RequestDetails objRequestDetails = new RequestDetails();

    public void Check()
    {
        string[] parms = DecryptionParms.Split('#');
        bool relParms = true;
        long[] longParms = new long[3];
        for (int i = 0; i < 3; i++)
        { 
            if (string.IsNullOrEmpty(parms[i]))
            {
                relParms = false;
                break;
            }
            else
            {
                relParms = long.TryParse(parms[i], out longParms[i]);
                if (!relParms)
                    break;
            }
        }
        if (relParms)
        {
            objRequestDetails.Visitor = longParms[0];
            objRequestDetails.VisitorPage = longParms[1];
            objRequestDetails.VisitorPage = longParms[2];
            objRequestDetails.ImageOutCode = string.Empty;
            objRequestDetails.Behavior = 0;  //访问
            try
            {
                lock (objRequestDetails)
                {
                    StatisticsManager.AddRequestDetails(objRequestDetails);
                    Thread.Sleep(1000);
                }
                Response.Write(" ");
            }
            catch (SqlException se)
            {
                Response.Write(se.Message);
            }
        }
    }

    /// <summary>
    /// 检查参数是否传递过来
    /// </summary>
    /// <returns></returns>
    protected bool CheckParms()
    {
        if (string.IsNullOrEmpty(Request.QueryString["tosee"]))
            return false;
        else
            return true;
    }



------解决方案--------------------
HttpContext.Current.Request
------解决方案--------------------
线程里面不是Request请求,哪来的 HttpContext.Current.Request啊