Ich bin Anfänger auf Stack-Überlauf und in ASP.NET im Allgemeinen, aber ich werde versuchen, meinen Punkt hier klar zu machen.Web-API Definieren von Routen zu Controllern asp.net
Ich entwickle eine Web-API in VB.NET, aber ich stecke fest, wenn ich versuche, Routen zu definieren.
ich zum Beispiel haben die folgenden Funktionen:
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
Wenn ich diesen Controller versuchen, mit Postbote, ich Firsty die GET-Methode überprüfen Sie die URI:/api/informationen. Die GetAllInformations() -Methode wird korrekt ausgelöst. Aber wenn ich die GET-Methode für eine bestimmte Informationseinheit auf diese Art von URI:/api/informations/i versuche, wird auch GetAllInformations() ausgelöst.
Ich habe diese Informationen aus dem Ereignisprotokoll in Visual Studio bekam:
"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"
}
}
Ich weiß nicht, warum die Anfrage an meine GetInformations Routing nicht korrekt ist (p_id Als Int16) -Funktion. Könnten Sie mir bitte hier helfen?
FYI: Ich habe diese Grund Routen Konfiguration:
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
EDIT:
Ich versuchte, ein Verfahren zu implementieren, beiden Fälle zu behandeln, mit einem optionalen Argumente, aber der Parameter nicht erkannt wird, Ereignis, wenn ich testen Sie die URI:/api/informationen/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
Ja, das ist es. markiere deinen Post als Antwort –
Du hättest auch deine Route ändern können um/{p_id} anstatt {id} zu sein. – Spivonious