Mark
Mark

Reputation: 7818

ASP.Net WebAPI URL not returning any data

I'm using the Northwind database, to try to understand the WebAPI for Asp.Net.

My Model/Controller/Global files are shown below.

When I navigate to localhost: xxxx/api/employees/5 it correctly goes to the correct controller:

' GET /api/employees/5
Public Function GetValue(ByVal ID As Integer) As Employee
    Dim emp As Employee = db.Employees.Find(ID)
    Return emp
End Function

...however emp returns Null/Nothing.

The Northwind database definitely has data:

Northwind snapshot

Could anyone see where I'm going wrong please?

Thanks, Mark


Models/Employee.vb:

Imports System.Data.Entity

  Namespace MvcApplication21
  Public Class Employee
    Public Property EmployeeID() As Integer
    Public Property FirstName() As String
    Public Property LastName() As String
  End Class

  Public Class EmployeeDBContext
    Inherits DbContext
    Public Property Employees() As DbSet(Of Employee)
  End Class
End Namespace

Controllers/EmployeesController.vb

Imports System.Web.Http
Imports MvcApplication21
Imports MvcApplication21.MvcApplication21

Public Class EmployeesController
Inherits ApiController

Private db As New EmployeeDBContext

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

' GET /api/employees/5
Public Function GetValue(ByVal EmployeeID As Integer) As Employee
    Dim employee As Employee = db.Employees.Find(EmployeeID)
    Return employee
End Function

Web.config:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Global.asax.vb

Imports System.Web.Http
Imports System.Web.Optimization

Public Class WebApiApplication
 Inherits System.Web.HttpApplication

 Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
    filters.Add(New HandleErrorAttribute())
 End Sub

 Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

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

    routes.MapRoute( _
        name:="Default", _
        url:="{controller}/{action}/{id}", _
        defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
    )
 End Sub

Upvotes: 1

Views: 1792

Answers (1)

Nicholas Murray
Nicholas Murray

Reputation: 13533

There is a conflict between your routing parameters and your function parameter

in the first part of your question you have

Public Function GetValue(ByVal ID As Integer) As Employee

in the controller it is

Public Function GetValue(ByVal EmployeeID As Integer) As Employee

  • one has ID as a parameter and the other has EmployeeID as the parameter

Whereas you are using ID in your routing

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

Upvotes: 1

Related Questions