如何获取 URL 和查询字符串? VB.Net

发布于 2024-07-26 05:46:53 字数 322 浏览 3 评论 0原文

我正在重构一些遗留代码。 该应用程序未使用查询字符串。 以前的开发人员对应用程序在其他地方使用的一些变量进行了硬编码。

像这样使用 VB.NET

 so.Cpage = "ContractChange.aspx"

我的问题是我可以通过编程方式设置这个值并包含当前的查询字符串吗?

我希望 so.Cpage 类似于 ContractChange.aspx?d=1&b=2

我可以用请求对象或其他东西来做到这一点吗? 请注意,我不需要域。

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

 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

Can I do this with the request object or something? Note, I don't need the domain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女 2024-08-02 05:46:53

要获取当前查询字符串,您只需执行类似以下操作:

Dim query as String = Request.QueryString("d")

这会将“d”查询字符串的值分配给字符串变量“query”。 请注意,所有查询字符串值都是字符串,因此如果您传递数字,则需要“转换”或将这些字符串值转换为数字(但在转换时请注意异常)。 例如:

Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)

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

使用这些机制(或非常类似的机制)应该使您能够构造一个字符串变量,其中包含您希望导航到的完整 url(页面和查询字符串)。

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:

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:

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.

彼岸花ソ最美的依靠 2024-08-02 05:46:53

尝试这个:

so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)

Try this:

so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
坏尐絯 2024-08-02 05:46:53

在 VB.Net 中,您可以使用以下命令来完成此操作。

Dim id As String = Request.Params("RequestId")

如果您想将其作为整数处理,可以执行以下操作:

Dim id As Integer

If Integer.TryParse(Request.Params("RequestId"), id) Then
   DoProcessingStuff()
End If

In VB.Net you can do it with the following.

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
左秋 2024-08-02 05:46:53

尝试这个

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

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
月朦胧 2024-08-02 05:46:53

不确定 VB.NET 中的语法,但在 C# 中您只需要这样做

StringId = Request.QueryString.Get("d");

希望这会有所帮助。

Not sure about the syntax in VB.NET but in C# you would just need to do

StringId = Request.QueryString.Get("d");

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文