我如何从网络钩子中使用 Json 负载并在 vb.net MVC 中显示 Json 的某些功能?
How can I consume a Json Payload from a webhook and display certain features of the Json in vb.net MVC?
我目前正在开发一个 ASP.NET visual basic MVC 应用程序,它将使用从 JIRA 触发的 webhook,然后它将在应用程序中显示该信息的某些位。
到目前为止,我已经成功地让 Webhook 成功触发并且 Webhook URL 将发送到 RequestB.in 并且它将发送 Json 有效载荷(目前发送到 RequestB.in),我可以看到它正在工作。
然后使用它发送给我的 Json 将其转换为 VB.Net classes 使用 Paste Json as Classes (这是我要显示的信息):
Public Class JiraWebhookObject
'properties
End Class
Public Class Issue
'properties
End Class
Public Class Fields
'Properties
End Class
Public Class Issuetype
'Properties
End Class
Public Class Priority
'Properties
End Class
Public Class Status
'Properties
End Class
Public Class Project
'Properties
End Class
Public Class Comment
'Properties
End Class
Public Class Comment1
'Properties
End Class
然后我创建了一个 API 控制器来将其连接到 class,如下所示:
Public Class 值控制器
继承 ApiController
'GET api/values
Public Function [Get]() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
'GET api/values/5
Public Function [Get](id As Integer) As String
Return "value"
End Function
'POST api/values
Public Sub Post(<FromBody> JiraWebhookObject As String)
End Sub
End Class
但是我如何在应用程序中为每个被触发的 Json 有效负载显示它。我正在尝试像这样反序列化数据:
Request.InputStream.Position = 0
Request.InputStream.Seek(0, SeekOrigin.Begin)
Using reader = New StreamReader(Request.InputStream)
Dim json = reader.ReadToEnd()
Dim contentType As String = Request.ContentType
Dim body = JsonConvert.DeserializeObject(json)
Try
Select Case DirectCast(body.key, String)
Case ""
'Return json(success = True)
Case Else
'Return json(New With {Key .success = True, Key .message = "Some message"})
End Select
Catch ex As Exception
End Try
End Using
但是在 Switch 语句中我不确定如何以正确的格式显示 Json。我必须使用事件来显示此 Json 数据吗?如何实现?
我是否需要像这样从 RequestB.in URL 中检索它:
Private Shared Sub test()
Dim task = MakeRequestJira()
task.Wait()
Dim response = task.Result
Dim body = response.Content.ReadAsStringAsync().Result
End Sub
Private Shared Async Function MakeRequestJira() As Task(Of HttpResponseMessage)
Dim httpClient = New HttpClient()
Return Await httpClient.GetAsync(New Uri("http://requestb.in/x9xnl8w9"))
End Function
我能否让 Webhook URL 触发我的应用程序(我应该指定什么 URL)然后使用它来检索信息?
这就是我从 Webhook URL 获得 Json 有效负载的方式:
Function URLEndpoint() As ActionResult
Dim r As System.IO.StreamReader = New System.IO.StreamReader(HttpContext.Request.InputStream)
Dim jsonAs String = r.ReadToEnd()
Dim issue As oject= JsonConvert.DeserializeObject(Of object)(json)
System.Diagnostics.Trace.TraceError(json)
然后创建 类 以从 Webhook 获取正确的信息:
Public Class object
Public Property issue As Issue
<DataMember(Name:="Timestamp")>
Public Property timestamp As Long
Public Property comment As Comment3
End Class
然后你可以对数据做你想做的事,例如显示它或通过电子邮件发送它
我目前正在开发一个 ASP.NET visual basic MVC 应用程序,它将使用从 JIRA 触发的 webhook,然后它将在应用程序中显示该信息的某些位。
到目前为止,我已经成功地让 Webhook 成功触发并且 Webhook URL 将发送到 RequestB.in 并且它将发送 Json 有效载荷(目前发送到 RequestB.in),我可以看到它正在工作。
然后使用它发送给我的 Json 将其转换为 VB.Net classes 使用 Paste Json as Classes (这是我要显示的信息):
Public Class JiraWebhookObject
'properties
End Class
Public Class Issue
'properties
End Class
Public Class Fields
'Properties
End Class
Public Class Issuetype
'Properties
End Class
Public Class Priority
'Properties
End Class
Public Class Status
'Properties
End Class
Public Class Project
'Properties
End Class
Public Class Comment
'Properties
End Class
Public Class Comment1
'Properties
End Class
然后我创建了一个 API 控制器来将其连接到 class,如下所示: Public Class 值控制器 继承 ApiController
'GET api/values
Public Function [Get]() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
'GET api/values/5
Public Function [Get](id As Integer) As String
Return "value"
End Function
'POST api/values
Public Sub Post(<FromBody> JiraWebhookObject As String)
End Sub
End Class
但是我如何在应用程序中为每个被触发的 Json 有效负载显示它。我正在尝试像这样反序列化数据:
Request.InputStream.Position = 0
Request.InputStream.Seek(0, SeekOrigin.Begin)
Using reader = New StreamReader(Request.InputStream)
Dim json = reader.ReadToEnd()
Dim contentType As String = Request.ContentType
Dim body = JsonConvert.DeserializeObject(json)
Try
Select Case DirectCast(body.key, String)
Case ""
'Return json(success = True)
Case Else
'Return json(New With {Key .success = True, Key .message = "Some message"})
End Select
Catch ex As Exception
End Try
End Using
但是在 Switch 语句中我不确定如何以正确的格式显示 Json。我必须使用事件来显示此 Json 数据吗?如何实现?
我是否需要像这样从 RequestB.in URL 中检索它:
Private Shared Sub test()
Dim task = MakeRequestJira()
task.Wait()
Dim response = task.Result
Dim body = response.Content.ReadAsStringAsync().Result
End Sub
Private Shared Async Function MakeRequestJira() As Task(Of HttpResponseMessage)
Dim httpClient = New HttpClient()
Return Await httpClient.GetAsync(New Uri("http://requestb.in/x9xnl8w9"))
End Function
我能否让 Webhook URL 触发我的应用程序(我应该指定什么 URL)然后使用它来检索信息?
这就是我从 Webhook URL 获得 Json 有效负载的方式:
Function URLEndpoint() As ActionResult
Dim r As System.IO.StreamReader = New System.IO.StreamReader(HttpContext.Request.InputStream)
Dim jsonAs String = r.ReadToEnd()
Dim issue As oject= JsonConvert.DeserializeObject(Of object)(json)
System.Diagnostics.Trace.TraceError(json)
然后创建 类 以从 Webhook 获取正确的信息:
Public Class object
Public Property issue As Issue
<DataMember(Name:="Timestamp")>
Public Property timestamp As Long
Public Property comment As Comment3
End Class
然后你可以对数据做你想做的事,例如显示它或通过电子邮件发送它