Search code examples
vb.netasp.net-web-apiasp.net-web-api-routing

The requested resource does not support http method 'GET' in vb.net web api


I am working on Web API 2 in vb.net and I am getting issue with GET method. First of all I am able able to put HttpGet or AcceptVerbs on either class or action method

I don't have Routeconfig because I created Web API 2 employ template project.

Here my WebApiConfig file

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

and api controller class

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

but When I run url : http://localhost:43021/api/home/ConcatValues?param1=1&param2=2

I am getting error :

{"Message":"The requested resource does not support http method 'GET'."}


Solution

  • Add the <HttpGet()> attribute to the action so that the convention based routing that you configured knows to associate the action with a GET request. Normally the convention inspects the action's name like GetConcatValues to determine by convention that it is a GET request. Since the example action is not employing that convention that the next option is to attach <HttpGet()> attribute to the action definition.

    ' 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