我需要一种将复杂的 Web 请求转换为 .Net 对象的方法。我知道 WebAPI 使用默认模型绑定器,对于复杂的数据,需要自定义模型绑定器。
对象
Public Class LapMobileModel
Public Property Type As Integer
Public Property EndTime As String
Public Property StartTime As String
End Class
Public Class RaceMobileModel
Public Property RaceName As String
Public Property UserId As Integer
Public Property Laps As IEnumerable(Of LapMobileModel)
Public Property numberOfRacers As String
Public Property PreRacePosition As String
Public Property PostRacePosition As String
End Class
Public Class RaceListMobileModel
Public Property RaceList As IEnumerable(Of RaceMobileModel)
End Class
动作(在 ApiController 中)
Public Function SyncLapData(ByVal raceList As RaceListMobileModel) As String
'stuff
Return "OK"
End Function
我有一个自定义模型绑定器的骨架:
Imports System.Web.Http
Imports System.Web.Http.ModelBinding
Imports System.Web.Http.Controllers
Public Class EventDataModelBinder
Implements IModelBinder
Public Function BindModel(actionContext As HttpActionContext,
bindingContext As ModelBindingContext)
As Boolean Implements IModelBinder.BindModel
End Function
End Class
问题:
如何使用actionContext
获取构建所需的数据RaceListMobileModel
?
如何正确地将其存储在bindingContext
?
现在,数据正在通过带有 JSON 内容的 POST 发送。