Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full query string in C# ASP.NET

Tags:

c#

url

asp.net

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

like image 532
Antony Carthy Avatar asked Dec 10 '09 09:12

Antony Carthy


People also ask

How do you use QueryString?

A Query String Collection is used to retrieve the variable values in the HTTP query string. If we want to transfer a large amount of data then we can't use the Request. QueryString. Query Strings are also generated by form submission or can be used by a user typing a query into the address bar of the browsers.

What is request QueryString?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).


1 Answers

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

like image 87
nitzmahone Avatar answered Sep 27 '22 21:09

nitzmahone