如何在 WebApi 中路由两个具有相同参数的 get 方法?
How to route two get methods with same params in WebApi?
在我的 ApiController
中,我有一个 GET 方法,它 return 从数据库中获取一些数据。
现在我必须通过添加 一个 GET 方法来扩大该控制器,该方法将 return 来自同一数据库的数据,但以另一种方式格式化,因此参数 我传递给该方法的方法与第一个方法相同。
我正在尝试执行以下操作:
Public Class CshController
Inherits ApiController
Public Function GetValoriCsh(ByVal npv As String, ByVal nc As String) As IEnumerable(Of Cshlvl)
Dim csh As Cshlvl = New Cshlvl
Return csh.ValoreCsh(npv, nc)
End Function
Public Function GetOperazioni(ByVal npv As String, ByVal nc As String) As IEnumerable(Of OperazioniCsh)
Dim operazioni As OperazioniCsh = New OperazioniCsh
Return operazioni.OperazioniCsh(npv, nc)
End Function
End Class
所以问题来了,api 失败了,因为有两种方法需要相同的参数,所以它不知道如何选择我要使用的方法。
实际上我正在通过以下 url api/csh/ 调用以下 api/ 是否可以通过调用 api/csh/ 以某种方式从 GetValoriCsh 获取数据并通过调用类似 api/csh/operazioni/ 的方式从 GetOperazioni?
获取数据
我的 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
我尝试在 GetOperazioni 上方添加 <Route("api/csh/op")>
,但没有效果。
如果使用属性路由,则全有或全无。
<RoutePrefix("api/csh")>
Public Class CshController
Inherits ApiController
'GET api/csh
<HttpGet()>
<Route("")>
Public Function GetValoriCsh(ByVal npv As String, ByVal nc As String) As IEnumerable(Of Cshlvl)
Dim csh As Cshlvl = New Cshlvl
Return csh.ValoreCsh(npv, nc)
End Function
'GET api/csh/op
<HttpGet()>
<Route("op")>
Public Function GetOperazioni(ByVal npv As String, ByVal nc As String) As IEnumerable(Of OperazioniCsh)
Dim operazioni As OperazioniCsh = New OperazioniCsh
Return operazioni.OperazioniCsh(npv, nc)
End Function
End Class
在我的 ApiController
中,我有一个 GET 方法,它 return 从数据库中获取一些数据。
现在我必须通过添加 一个 GET 方法来扩大该控制器,该方法将 return 来自同一数据库的数据,但以另一种方式格式化,因此参数 我传递给该方法的方法与第一个方法相同。
我正在尝试执行以下操作:
Public Class CshController
Inherits ApiController
Public Function GetValoriCsh(ByVal npv As String, ByVal nc As String) As IEnumerable(Of Cshlvl)
Dim csh As Cshlvl = New Cshlvl
Return csh.ValoreCsh(npv, nc)
End Function
Public Function GetOperazioni(ByVal npv As String, ByVal nc As String) As IEnumerable(Of OperazioniCsh)
Dim operazioni As OperazioniCsh = New OperazioniCsh
Return operazioni.OperazioniCsh(npv, nc)
End Function
End Class
所以问题来了,api 失败了,因为有两种方法需要相同的参数,所以它不知道如何选择我要使用的方法。
实际上我正在通过以下 url api/csh/ 调用以下 api/ 是否可以通过调用 api/csh/ 以某种方式从 GetValoriCsh 获取数据并通过调用类似 api/csh/operazioni/ 的方式从 GetOperazioni?
获取数据我的 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
我尝试在 GetOperazioni 上方添加 <Route("api/csh/op")>
,但没有效果。
如果使用属性路由,则全有或全无。
<RoutePrefix("api/csh")>
Public Class CshController
Inherits ApiController
'GET api/csh
<HttpGet()>
<Route("")>
Public Function GetValoriCsh(ByVal npv As String, ByVal nc As String) As IEnumerable(Of Cshlvl)
Dim csh As Cshlvl = New Cshlvl
Return csh.ValoreCsh(npv, nc)
End Function
'GET api/csh/op
<HttpGet()>
<Route("op")>
Public Function GetOperazioni(ByVal npv As String, ByVal nc As String) As IEnumerable(Of OperazioniCsh)
Dim operazioni As OperazioniCsh = New OperazioniCsh
Return operazioni.OperazioniCsh(npv, nc)
End Function
End Class