我刚刚开始学习Web API2控制器,并且遇到了一些困难。在C#中,它可以很好地调用所有记录和过滤,但是使用VB.Net可以很好地获取所有记录但是在过滤特定记录时它不起作用。以下是代码:
C#代码正常工作:
public class EmployeesController : ApiController
{
public IEnumerable<Employee> Get()
{
using (EmployeeDBEntities entities = new EmployeeDBEntities())
{
return entities.Employees.ToList();
}
}
public Employee Get(int id)
{
using (EmployeeDBEntities entities = new EmployeeDBEntities())
{
return entities.Employees.FirstOrDefault(e => e.ID == id);
}
}
}
VB.Net代码仅在获取所有记录时才能正常工作,但在过滤特定记录时则无效:
Public Class EmployeesController
Inherits ApiController
Public Function GetValues() As IEnumerable(Of Employee)
Using entities As EmployeeDBEntities = New EmployeeDBEntities()
Return entities.Employees.ToList
End Using
End Function
Public Function GetValues(vID As Integer) As Employee
Using entities As EmployeeDBEntities = New EmployeeDBEntities()
Return entities.Employees.FirstOrDefault(Function(e) e.ID = vID)
End Using
End Function
End Class
非常感谢您的帮助!