vb.net 如何获取 URL 和查询字符串?网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1092859/
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
How can I get the URL and Querystring? vb.net
提问by Hcabnettek
I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.
我正在重构一些遗留代码。该应用程序未使用查询字符串。之前的开发人员正在硬编码应用程序在其他地方使用的一些变量。
Like this using VB.NET
像这样使用 VB.NET
so.Cpage = "ContractChange.aspx"
My question is can I programatically set this value and include the current querystring?
我的问题是我可以以编程方式设置此值并包含当前查询字符串吗?
I want so.Cpage
to be something like ContractChange.aspx?d=1&b=2
我想so.Cpage
成为这样的人ContractChange.aspx?d=1&b=2
Can I do this with the request object or something? Note, I don't need the domain.
我可以用请求对象或其他东西来做到这一点吗?请注意,我不需要域。
采纳答案by Sani Singh Huttunen
Try this:
尝试这个:
so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
回答by CraigTP
To get the current query string you would simply do something like the following:
要获取当前查询字符串,您只需执行以下操作:
Dim query as String = Request.QueryString("d")
This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:
这会将“d”查询字符串的值分配给字符串变量“query”。请注意,所有查询字符串值都是字符串,因此如果您要传递数字,则需要“转换”或将这些字符串值转换为数字(不过,在转换时要小心例外)。例如:
Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)
The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:
Request 对象的 QueryString 属性是名称/值键对的集合。具体来说,它是 System.Collections.Specialized.NameValueCollection 类型,您可以像这样遍历每个名称/值对:
Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
value = coll(key)
Next
Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.
使用这些机制中的任何一种(或非常相似的东西)应该使您能够构造一个字符串变量,其中包含您希望导航到的完整 url(页面和查询字符串)。
回答by Dillie-O
In VB.Net you can do it with the following.
在 VB.Net 中,您可以使用以下方法进行操作。
Dim id As String = Request.Params("RequestId")
If you want to process this in as an integer, you can do the following:
如果要将其作为整数处理,可以执行以下操作:
Dim id As Integer
If Integer.TryParse(Request.Params("RequestId"), id) Then
DoProcessingStuff()
End If
回答by Yustian
try this
尝试这个
Dim name As String = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
Dim qrystring As String = Request.ServerVariables("QUERY_STRING")
Dim fullname As String = name & "/" & qrystring
回答by Yustian
Not sure about the syntax in VB.NET but in C# you would just need to do
不确定 VB.NET 中的语法,但在 C# 中你只需要做
string id = Request.QueryString.Get("d");
string id = Request.QueryString.Get("d");
Hope this helps.
希望这可以帮助。