I am using Web API in my Web Forms project. I have the following code in my project’s Application_Start method in Global.asax:
GlobalConfiguration.Configuration.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", New With {.id = RouteParameter.Optional})
This is basically copied and pasted from a Microsoft tutorial on the subject.
I also have a test controller named ValuesController. This class is just the default Web API controller one gets when creating a controller from the Add New Item wizard and is in a folder in my Web Forms site named Controllers:
Imports System.Net
Imports System.Web.Http
Public Class ValuesController
Inherits ApiController
' GET api/<controller>
Public Function GetValues() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
' GET api/<controller>/5
Public Function GetValue(ByVal id As Integer) As String
Return "value"
End Function
' POST api/<controller>
Public Sub PostValue(<FromBody()> ByVal value As String)
End Sub
' PUT api/<controller>/5
Public Sub PutValue(ByVal id As Integer, <FromBody()> ByVal value As String)
End Sub
' DELETE api/<controller>/5
Public Sub DeleteValue(ByVal id As Integer)
End Sub
End Class
HOWEVER – when I go to http://localhost/api/Values – instead of seeing some XML serializing the strings value1 and value2, I see an error message like so:
<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost/api/Values'.</Message>
<MessageDetail>No type was found that matches the controller named 'Values'.</MessageDetail>
</Error>
So – clearly, routing is working, since instead of getting a 404 I get a message saying that the route itself doesn’t resolve to anything. But the route should resolve to something – specifically, my ValuesController class which is even in a folder named Controllers.
Anyone know what I’m doing wrong?
Thanks in advance.
I figured it out. It’s because apparently MVC requires that the Controllers folder be at the same level as the file declaring the routes – not (as I previously thought) simply at the root level.
Ugh.