This project is read-only.


Topics: Web Api
Jan 25, 2012 at 1:10 AM

I'm trying to accessing the current QueryString with:

Dim context = System.Web.HttpContext.Current
Dim queryString As NameValueCollection = context.Request.QueryString

But I've noticed the query string only contains the ASP.NET session ID and not the query string like it does if you call the above code from anywhere else except inside a class being used a service contract.

Is there anyway to get them without having to pick them out manually from the uri?


Jan 25, 2012 at 1:11 AM

In what context are you accessing the querystring? Always access the request via the HttpRequestMessage and HttpResponseMessage objects. Do not use the static context classes.

Jan 25, 2012 at 1:13 AM

The querystring is not built into a name/value pair, if that is what you are looking for. You only get the RequestUri which has the segments, and the query.

You can use this extension to get the querystring values from the HttpRequestMessage object. (Warning: it is not very efficient for multiple calls)

    public static class HttpRequestMessageExtensions
        public static string QueryString(this HttpRequestMessage request, string name)
            string requestUri = request.RequestUri.Query;
            string[] queries = requestUri.Split('&');

            foreach (string s in queries)
                if (s.Contains(name))
                    int index = s.IndexOf('=');
                    int stopIndex = s.IndexOf('&', index);
                    if (stopIndex != -1)
                        return s.Substring(index + 1, stopIndex - index - 1);
                        return s.Substring(index + 1, s.Length - index - 1);

            return string.Empty;