gpt4 book ai didi

vb.net - 如何将多个属性传递给 WebApi?

转载 作者:行者123 更新时间:2023-12-04 11:00:43 25 4
gpt4 key购买 nike

我有一个 webapi 制造于 VB.NET 我可以在其中获取所有数据,按月和按年获取数据。现在我正在尝试添加通过数据范围获取数据的功能,在构造函数中,该方法如下所示:

Public Function TotaliDataData(ByVal inizio As String, ByVal fine As String) As IEnumerable(Of Totali)
Dim SQLConnect As String = "Server=127.0.0.1;Port=3306;Database=00168780351;Uid=root;Pwd=block;"
Dim cn As New MySqlConnection(SQLConnect)
Dim cmd As New MySqlCommand("SELECT NPV_TT, NCASSA_TT, DATA_TT, AZZ_TT, SUM(NSC_TT) AS NSC_TT, SUM(VENDITE_TT) AS VENDITE_TT, RESI_TT, ANNULLI_TT, SCONTI_TT, FATTURE_TT, NC_TT FROM totali WHERE DATA_TT BETWEEN '" & inizio & "' AND '" & fine & "' GROUP BY MONTH(DATA_TT);", cn)
cn.Open()
Dim reader As MySqlDataReader = cmd.ExecuteReader()
Dim totali As List(Of Totali) = ConvertReader(reader)
cn.Dispose()
Return totali
End Function

这是我的 TotaliController.VB:
Public Class TotaliController
Inherits ApiController

Public Function GetTotali() As IEnumerable(Of Totali)
Dim Totali As Totali = New Totali
Return Totali.GetTotali()
End Function

Public Function GetTotaliByYear(ByVal anno As String) As IEnumerable(Of Totali)
Dim Totali As Totali = New Totali
Return Totali.TotaliAnno(anno)
End Function

Public Function GetTotaliByMonth(ByVal mese As String) As IEnumerable(Of Totali)
Dim Totali As Totali = New Totali
Return Totali.TotaliMese(mese)
End Function

Public Function TotaliDataData(ByVal inizio As String, ByVal fine As String) As IEnumerable(Of Totali)
Dim Totali As Totali = New Totali
Return Totali.TotaliDataData(inizio, fine)
End Function

Public Function GetDetails(ByVal annod As String) As IEnumerable(Of Totali)
Dim Totali As Totali = New Totali
Return Totali.GetDetails(annod)
End Function

End Class

然后我试图调用 localhost:port/api/totali/?inizio=2019-11-10&fine=2019-12-20但即使在 Controller 的调试中它也不会进入,而如果我尝试使用单个参数按月或按年获取数据,它工作正常..

那么如何将多个参数传递给 WebApi?

更新:

当我尝试运行 localhost:port/api/totali/?inizio=2019-11-10&fine=2019-12-20它从 Controller 调用默认的 GetTotali() 方法,该方法返回数据库中的所有数据。

全局的:
Public Class WebApiApplication
Inherits System.Web.HttpApplication

Protected Sub Application_Start()
GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
GlobalConfiguration.Configuration.Formatters.
JsonFormatter.MediaTypeMappings.Add(New RequestHeaderMapping("Accept", "text/html", StringComparison.InvariantCultureIgnoreCase, True, "application/json"))
End Sub

End Class

WebApiConfig
Public Module WebApiConfig
Public Sub Register(ByVal config As HttpConfiguration)
' Servizi e configurazione dell'API Web

' Route dell'API Web
config.MapHttpAttributeRoutes()

config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
End Sub
End Module

最佳答案

显然,WebApi Controller 中的方法名称必须以 REST 前缀开头,因此通过更改方法名称 TotaliDataDataGetTotaliDataData解决了这个问题。

关于vb.net - 如何将多个属性传递给 WebApi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com