request.querystring in vb.net



我有一个需要解码的url:

http://gistest:54321/default.aspx?data=%7B%22id%22:%2269403%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2223-10%2010:12%22%7D

此代码每次都会更改

我使用这个代码:

<%Response.Write(Request.QueryString.Item("data") )%><br/>
<%Response.Write(Request.QueryString.Item("id") )%><br/>
<%Response.Write(Request.QueryString.Item("longitude") )%><br/>
<%Response.Write(Request.QueryString.Item("latitude") )%><br/>
<%Response.Write(Request.QueryString.Item("timestamp") )%><br/>

但我只将其作为输出,也许有一个选项可以检查数据是否为空,然后我请求.querystring数据中的其他部分:

{"id"="69403","longitude"="-143.406417","latitude"="32.785834","timestamp"="23-10 10:12"}

这是来自

<%Response.Write(Request.QueryString.Item("data"))%>

我真的希望我能正确理解这个问题。我假设您需要名为Data的查询字符串键中的每个键的值?为此,我使用了以下代码:

Dim values() As String = Server.UrlDecode(Request.QueryString("data")).Replace("{", "").Replace("}", "").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {":"}, StringSplitOptions.RemoveEmptyEntries)
Response.Write(keyValue(0).Replace("""", "") & " : " & keyValue(1).Replace("""", "") & "<br/>")
Next

简而言之,我解码QueryString("data"),替换大括号,并使用逗号作为第一个拆分字符将字符串拆分为数组。然后,我们得到一个数组,该数组包含以下格式的值"id":"649403"。

此后,我对这些值进行迭代,并根据分号(:)字符为每个值拆分最后一次。

使用此方法,您可以动态地构建和操作数据。

代码编辑

我将":"的所有引用替换为"=",以确保正确检索时间戳,然后根据=拆分键值。如果需要,可以使用select case将变量分配给值。(显然,请确保您的变量没有在select中声明,因为它不在以后使用的正确范围内!)

If Request.QueryString("data") IsNot Nothing Then
Dim values() As String = Request.QueryString("data").Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
Response.Write(keyValue(0).Replace("""", "") & " : " & keyValue(1).Replace("""", "") & "<br/>")
Select Case keyValue(0).ToLower()
Case "id"
Dim id As String = keyValue(1)
End Select
Next
End If

反射编辑

创建对象的实例,然后检索其所有属性(Dim属性()As PropertyInfo=myObj.GetType().GetProperties())。遍历属性并设置名称等于键的值。不要忘记导入System.Reflection库。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim data As String = "%7B%22id%22:%2269403%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2223-10%2010:12%22%7D"
If data IsNot Nothing Then
Dim myObj As New MyObject
Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
Dim values() As String = Server.UrlDecode(data).Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
For Each prop As PropertyInfo In properties
If prop.Name.ToLower = keyValue(0).ToLower.Replace("""", "") Then
prop.SetValue(myObj, keyValue(1), Nothing)
End If
Next
Next
myObj.Save()
End If
End Sub

Public Class MyObject
Private _ID As String
Private _Longitude As String
Private _Latitude As String
Private _Timestamp As String
Public Property ID As String
Get
Return _ID
End Get
Set(value As String)
_ID = value
End Set
End Property
Public Property Longitude As String
Get
Return _Longitude
End Get
Set(value As String)
_Longitude = value
End Set
End Property
Public Property Latitude As String
Get
Return _Latitude
End Get
Set(value As String)
_Latitude = value
End Set
End Property
Public Property Timestamp As String
Get
Return _Timestamp
End Get
Set(value As String)
_Timestamp = value
End Set
End Property
Public Sub Save()
'Save logic here
End Sub
End Class

我认为您遇到的问题是,在您发布的URL中只有一个querystring参数,即data。其余信息编码在data查询字符串值中。存储在data中的值看起来几乎像一个JSON/Javascript对象,只是属性名称和值之间有一个=,而不是:

因此,基本上你将无法使用Request.QueryString来获得idlongitudelatitude等的值。我认为你的选择是要么自己编写一些代码来解析data的值,要么用:替换=,并使用.NET的JSON解析器(即JavascriptSerializer类或JSON.net)。

就我个人而言,我会在codeehind中编写一个方法,返回一个Dictionary(Of String, Object)。在这种方法中,我只需将每个"="更改为":",然后使用.NET提供的JavaScriptSerializer来解析字符串。我现在手头没有ASP.NET实例,但我在LinqPad中提供的以下示例应该可以说明这个想法:

Sub Main
Dim url = "http://gistest:54321/default.aspx?data=%7B%22id%22=%2269403%22,%22longitude%22=%22-143.406417%22,%22latitude%22=%2232.785834%22,%22timestamp%22=%2223-10%2010:12%22%7D"
Dim uri = New Uri(url)
Dim data = System.Web.HttpUtility.ParseQueryString(uri.Query)("data")
Dim o = ParseData(data)
Console.WriteLine(o("id"))
Console.WriteLine(o("longitude"))
Console.WriteLine(o("latitude"))
Console.WriteLine(o("timestamp"))
End Sub
Function ParseData(data As String) As Dictionary(Of String, Object)
Dim js = new System.Web.Script.Serialization.JavaScriptSerializer()
Dim o = js.DeserializeObject(data.Replace("""=""", """:"""))
ParseData = DirectCast(o, Dictionary(Of String, Object))
End Function

关于这种方法需要注意的一点是,我希望url的格式与您发布的相同。您可能需要修改此方法,使其更健壮,以处理不同的输入。

如果您将ParseData函数放入代码绑定中,则首页中的以下代码应该为您提供所需的输出(再次抱歉,我现在没有可供测试的ASP.NET实例):

<%
Dim o = ParseData(Request.QueryString.Item("data"))
Response.Write(o("id"))
Response.Write("<br />")
Response.Write(o("longitude"))
Response.Write("<br />")
Response.Write(o("latitude"))
Response.Write("<br />")
Response.Write(o("timestamp"))
Response.Write("<br />")
%>

相关内容

  • 没有找到相关文章

最新更新