私はスタックオーバーフローとASP.NETで一般的に初心者ですが、私はここで明確にするつもりです。Web APIコントローラへのルートを定義するasp.net
私はVB.NETでWeb APIを開発していますが、経路を定義しようとすると止まってしまいます。
私は、例えば、これらの機能があります。/ 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
、私はfirsty URIのためのGETメソッドを確認してください。 GetAllInformations()メソッドが正しくトリガされます。 しかし、この種のURI:/ api/informations/iの特定の情報項目に対してGETメソッドを実行しようとすると、GetAllInformations()もトリガーされます。
私は、Visual Studioでのイベントジャーナルからこれらの情報を持っている:
"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"
}
}
要求が正しく機能(Int16型としてP_IDを)私のGetInformationsにルーティングされていない理由を私は知りません。ここで私を助けてくれますか?
はFYI:私はこの基本的なルートの構成を有する:
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 /情報/私は
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
はい、それです。あなたの投稿を回答として –
とすると、{id}の代わりに/ {p_id}にルートを変更することもできます。 – Spivonious