我有一个用 VB.NET 制作的 webapi ,在这里我可以获取所有数据,按月获取数据,按年获取数据。现在,我尝试添加按数据范围获取数据的功能,在构造函数中,该方法如下所示:
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
,但即使在控制器的调试中也没有输入,而如果我尝试通过单个参数按月或年获取数据,则效果很好。
那么我如何将多个参数传递给WebApi?
更新:
当我尝试运行localhost:port/api/totali/?inizio=2019-11-10&fine=2019-12-20
时,它将从控制器调用默认的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
答案 0 :(得分:2)
显然,WebApi控制器中的方法名称必须以REST前缀开头,因此将方法TotaliDataData
更改为GetTotaliDataData
即可解决问题。