84

As a PHP programmer I'm used to using $_GET to retrieve the HTTP query string... and if I need the whole string, theres loads of ways to do it.

In ASP however, I can't seem to get the query.

Here is the code for news.aspx (embedded in some HTML):

<%                             
    string URL = "http://www.example.com/rendernews.php?"+Request.Querystring;
    System.Net.WebClient wc = new System.Net.WebClient();
    string data = wc.DownloadString(URL);
    Response.Output.Write(data);
%>

I am fetching a PHP script's output from a remote server, and this works perfectly without the Request.Querystring.

The issue is that I'm trying to get the full query string on the first line: Request.Querystring. I am getting an error "Object reference not set to an instance of an object" which basically means that Request.Querystring doesn't exist.

Any idea what the problem is here? How can I get that query string so when index.aspx is called like http://test.com/news.aspx?id=2 my script fetches http://www.example.com/rendernews.php?id=2

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Antony Carthy
  • 5,549
  • 9
  • 34
  • 38

6 Answers6

206

Try Request.Url.Query if you want the raw querystring as a string.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
nitzmahone
  • 13,720
  • 2
  • 36
  • 39
19

This should work fine for you.

Write this code in the Page_Load event of the page.

string ID = Request.QueryString["id"].ToString();
Response.Redirect("http://www.example.com/rendernews.php?id=" + ID);
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
11

Request.QueryString returns you a collection of Key/Value pairs representing the Query String. Not a String. Don't think that would cause a Object Reference error though. The reason your getting that is because as Mauro pointed out in the comments. It's QueryString and not Querystring.

Try:

Request.QueryString.ToString();

or

<%                                 
    string URL = Request.Url.AbsoluteUri 
    System.Net.WebClient wc = new System.Net.WebClient();
    string data = wc.DownloadString(URL);
    Response.Output.Write(data);
%>

Same as your code but Request.Url.AbsoluteUri will return the full path, including the query string.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Damien
  • 13,927
  • 14
  • 55
  • 88
  • With that I get: "'System.Web.HttpRequest' does not contain a definition for 'Querystring' and no extension method 'Querystring' accepting a first argument of type 'System.Web.HttpRequest'" and when I try adding System.Web before it, I get "The type or namespace name 'Request' does not exist in the namespace 'System.Web' (are you missing an assembly?) " – Antony Carthy Dec 10 '09 at 09:39
  • Might want to make sure you aren't overriding Request somewhere. I have something using Request.Querystring[.ToString()] in production and it works flawlessly. – Matt Dec 10 '09 at 09:50
  • 2
    isnt it QueryString and not Querystring? code above looks like c# - remember case sensitivity – Mauro Dec 10 '09 at 10:00
  • 1
    In my case, this is exactly what I want to avoid, because ToString() is buggy in the **** old version of the .NET framework that I'm forced to use. – Florian Winter Aug 14 '18 at 11:50
1

Just use Request.QueryString.ToString() to get full query string, like this:

string URL = "http://www.example.com/rendernews.php?"+Request.Querystring.ToString();
terR0Q
  • 1,369
  • 8
  • 17
0

I have tested your example, and while Request.QueryString is not convertible to a string neither implicit nor explicit still the .ToString() method returns the correct result.

Further more when concatenating with a string using the "+" operator as in your example it will also return the correct result (because this behaves as if .ToString() was called).

As such there is nothing wrong with your code, and I would suggest that your issue was because of a typo in your code writing "Querystring" instead of "QueryString".

And this makes more sense with your error message since if the problem is that QueryString is a collection and not a string it would have to give another error message.

yoel halb
  • 12,188
  • 3
  • 57
  • 52
-1

just a moment ago, i came across with the same issue. and i resolve it in the following manner.

Response.Redirect("../index.aspx?Name="+this.textName.Text+"&LastName="+this.textlName.Text);

with reference to the this

user2147280
  • 29
  • 1
  • 1
  • 3
  • 1
    Never never never build URLs like this in any framework or programming language. Use URL builders. They take care of encoding for you, which the code above doesn't do. – Florian Winter Aug 14 '18 at 11:51