2017-03-18 1 views
1

Ich arbeite an Web-API 2 in vb.net und ich bekomme ein Problem mit GET-Methode. Zunächst einmal ich bin in der Lage Lage HttpGet oder AcceptVerbs auf jede Klasse oder Aktionsmethode zu setzenDie angeforderte Ressource unterstützt http Methode 'GET' in vb.net Web API

ich nicht Routeconfig habe, weil ich Web-API 2 beschäftigt Vorlagenprojekt erstellt.

Hier meine WebApiConfig Datei

Public Module WebApiConfig 
    Public Sub Register(ByVal config As HttpConfiguration) 
     ' Web API configuration and services 

     ' Web API routes 
     config.MapHttpAttributeRoutes() 

     config.Routes.MapHttpRoute(
      name:="DefaultApi", 
      routeTemplate:="api/{controller}/{action}/{id}", 
      defaults:=New With {.id = RouteParameter.Optional} 
     ) 

     config.Formatters.JsonFormatter.SupportedMediaTypes.Add(New MediaTypeHeaderValue("text/html")) 
    End Sub 
End Module 

und api Controller-Klasse

Public Class HomeController 
    Inherits ApiController 

    ' GET api/values 
    'Public Function GetValues() As IEnumerable(Of String) 
    ' Return New String() { "value1", "value2" } 
    'End Function 


    ' GET api/values/5 

    Public Function ConcatValues(ByVal param1 As String,ByVal param2 As String) As String 
     Return "value" 
    End Function 

End Class 

aber Wenn ich laufen url: http://localhost:43021/api/home/ConcatValues?param1=1&param2=2

Ich erhalte Fehler:

{“ Nachricht ":" Der req uested Ressource nicht unterstützt HTTP-Methode ‚GET‘. "}

Antwort

1

Fügen Sie die <HttpGet()> Attribut auf die Aktion, so dass die Konvention based Routing, die Sie, die Aktion zu assoziieren mit einem GET Anforderung konfiguriert kennt. Normalerweise überprüft die Konvention den Namen der Aktion wie GetConcatValues, um per Konvention zu bestimmen, dass es sich um eine GET Anfrage handelt. Da die Beispielaktion diese Konvention nicht verwendet, besteht die nächste Option darin, das Attribut <HttpGet()> an die Aktionsdefinition anzufügen.

' GET api/home/concatvalues?param1=1&param2=2 
<System.Web.Http.HttpGet()> 
Public Function ConcatValues(ByVal param1 As String,ByVal param2 As String) As String 
    Return "value" 
End Function 
Verwandte Themen