在 C# ASP.NET 中获取完整的查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1879773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get full query string in C# ASP.NET
提问by Antony Carthy
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.
作为一名 PHP 程序员,我习惯于使用 $_GET 来检索 HTTP 查询字符串……如果我需要整个字符串,有很多方法可以做到。
In ASP however, I can't seem to get the query.
然而,在 ASP 中,我似乎无法获得查询。
Here is the code for news.aspx (embedded in some HTML):
这是 news.aspx 的代码(嵌入在一些 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.
我正在从远程服务器获取 PHP 脚本的输出,这在没有 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.
问题是我试图在第一行获取完整的查询字符串:Request.Querystring。我收到错误“未将对象引用设置为对象的实例”,这基本上意味着 Request.Querystring 不存在。
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=2my script fetches http://www.example.com/rendernews.php?id=2
知道这里有什么问题吗?我怎样才能得到那个查询字符串,所以当 index.aspx 被调用时像http://test.com/news.aspx?id=2我的脚本获取http://www.example.com/rendernews.php?id=2
采纳答案by nitzmahone
Try Request.Url.Query
if you want the raw querystring as a string.
Request.Url.Query
如果您想要原始查询字符串作为字符串,请尝试。
回答by Damien
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.
Request.QueryString
返回代表查询字符串的键/值对的集合。不是字符串。不要认为这会导致对象引用错误。您之所以会这样做,是因为正如 Mauro 在评论中指出的那样。它是 QueryString 而不是 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.
与您的代码相同,但Request.Url.AbsoluteUri
将返回完整路径,包括查询字符串。
回答by Naveed Butt
This should work fine for you.
这应该适合你。
Write this code in the Page_Load
event of the page.
Page_Load
在页面事件中编写此代码。
string ID = Request.QueryString["id"].ToString();
Response.Redirect("http://www.example.com/rendernews.php?id=" + ID);
回答by terR0Q
Just use Request.QueryString.ToString()
to get full query string, like this:
只需用于Request.QueryString.ToString()
获取完整的查询字符串,如下所示:
string URL = "http://www.example.com/rendernews.php?"+Request.Querystring.ToString();
回答by yoel halb
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.
我已经测试了您的示例,虽然 Request.QueryString 不能转换为既不隐式也不显式的字符串,但 .ToString() 方法仍然返回正确的结果。
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).
此外,在您的示例中使用“+”运算符连接字符串时,它还将返回正确的结果(因为这就像调用了 .ToString() 一样)。
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".
因此,您的代码没有任何问题,我建议您的问题是因为您的代码写错了“Querystring”而不是“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.
这对您的错误消息更有意义,因为如果问题是 QueryString 是一个集合而不是一个字符串,它就必须给出另一条错误消息。