Reputation: 113
I am trying to include Web APi in an ASp.NET application with Mvc. The application uses Identity Framework to authenticate itself.
I added a WebApiConfig
Imports System.Web.Http
Namespace ActualizadorApp.Api
Public NotInheritable Class WebApiConfig
Private Sub New()
End Sub
Public Shared Sub Register(config As HttpConfiguration)
' TODO: Add any additional configuration code.
' Web API routes
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(name:="Api", routeTemplate:="api/{controller}/{id}", defaults:=New With {
Key .id = RouteParameter.[Optional]
})
' WebAPI when dealing with JSON & JavaScript!
' Setup json serialization to serialize classes to camel (std. Json format)
Dim formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter
formatter.SerializerSettings.ContractResolver = New Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
End Sub
End Class
End Namespace
In Global.Asax I have referenced this configuration
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
RegisterRoutes(RouteTable.Routes)
WebApiConfig.Register(GlobalConfiguration.Configuration)
ModelBinders.Binders.Add(GetType(Decimal), New DecimalModelBinder())
ModelBinders.Binders.Add(GetType(Decimal?), New DecimalModelBinder())
End Sub
The authentication through / token is correct and returns the token correctly, but in the following GET calls to drivers the client returns me a 404 Could someone tell me that I'm doing wrong?
Imports System.Web.Http
<Authorize()>
Public Class TestController
Inherits ApiController
'public TestController() { }
' GET api/test
Public Function GetValues() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
' GET api/test/5
Public Function GetValue(id As Integer) As String
Return "value"
End Function
End Class
Upvotes: 2
Views: 873
Reputation: 246998
Web Api need to be registered before MVC Routes. And also you need to switch around the GlobalConfiguration
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
'Regsiter Web API routes before MVC routes
GlobalConfiguration.Configure(WebApiConfig.Register)
'MVC routes
RegisterRoutes(RouteTable.Routes)
ModelBinders.Binders.Add(GetType(Decimal), New DecimalModelBinder())
ModelBinders.Binders.Add(GetType(Decimal?), New DecimalModelBinder())
End Sub
Upvotes: 1