无法获取查询字符串

时间:2012-04-16 12:17:32

标签: asp.net

我使用以下代码在当前的http上下文中设置URL

public HttpContext SetNewHttpContext(string uRL)
    {
        var httpRequest = new HttpRequest("", uRL, "");
        var httpResponse = new HttpResponse(new StringWriter());         
        return new HttpContext(httpRequest, httpResponse);
    }

下调用它
HttpContext.Current = SetNewHttpContext("http://root/test.aspx?userid=319279549&name=xyz");

var val =  HttpContext.Current.Request.QueryString["userid"];

但我无法获取查询字符串的值(此处为userid)并且为null。

为什么?

请帮忙

1 个答案:

答案 0 :(得分:1)

   Uri tempUri = new Uri("http://root/test.aspx?userid=319279549&name=xyz"); 
   string sQuery = tempUri.Query;

   NameValueCollection queryString =
                System.Web.HttpUtility.ParseQueryString(sQuery ,Encoding.UTF8);