我是初学者的堆栈溢出和一般的ASP.NET,但我会试着在这里说清楚。
我正在VB.NET中开发一个Web API,但是当我尝试定义路由时我就陷入了困境。
我有这些功能:
Public Function GetAllInformations() As IEnumerable(Of cl_information)
'return all informations
End Function
Public Function GetInformations(p_id As Int16) As IHttpActionResult
'return a specific informations
End Function
Public Function PutInformation(p_information As cl_information) As IHttpActionResult
'return the http statuscode depending on the update of the information
End Function
Public Function PostInformation(p_information As cl_information) As IHttpActionResult
'return the http statuscode depending on the post of the information
End Function
当我使用postman尝试这个控制器时,我第一次检查GET方法的URI:/ api / informations。 GetAllInformations()方法被正确触发。 但是当我为特定的信息项尝试GET方法时,在这种URI:/ api / informations / i上,GetAllInformations()也会被触发。
我从视觉工作室的活动期刊获得了这些信息:
"data": {
"baseType": "RequestData",
"baseData": {
"ver": 2,
"id": "12785441767974844366",
"name": "GET informations [id]",
"startTime": "2016-05-12T08:56:49.4044704+02:00",
"duration": "00:00:04.1740006",
"success": true,
"responseCode": "200",
"url": "http://localhost:51651/api/informations/i",
"httpMethod": "GET",
"properties": {
"DeveloperMode": "true"
}
}
我不知道为什么请求没有正确路由到我的GetInformations(p_id As Int16)函数。你能帮帮我吗?
仅供参考:我有这个基本路线配置:
Public Module WebApiConfig
Public Sub Register(ByVal config As HttpConfiguration)
' Configuration et services API Web
' Itinéraires de l'API Web
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
End Sub
End Module
编辑:
我尝试用一个可选参数来实现一个处理这两种情况的方法,但是如果我测试了URI则没有检测到参数:/ api / informations / i
Public Function GetInformations(Optional p_id As Int16 = 0) As IHttpActionResult
If p_id = 0 Then
'return all informations
End If
'return a specific information
End Function
答案 0 :(得分:0)
我使用了错误的参数名称:
Public Function GetInformations(p_id As Int16) As IHttpActionResult
所以我改变了:
Public Function GetInformations(id As Int16) As IHttpActionResult
它正在工作。