REST URI参数来过滤响应

问题描述:

我想根据从REST URI检索到的参数过滤从数据库检索的对象列表。示例URI将为http://127.0.0.1:8080/api/employee?FirstName=Abigail&LastName=Ybarra,它将检索具有指定名字和姓氏的对象列表。REST URI参数来过滤响应

同时击中这样的URI,我得到的错误是:

<ExceptionMessage> 
Object reference not set to an instance of an object 
</ExceptionMessage> 
<ExceptionType>System.NullReferenceException</ExceptionType> 

MyWebService.Controllers.EmployeeApiController+<>c__DisplayClass4_0.<ListEmployees>b__0 (MyWebService.Models.Employee x) [0x00000] in <8bf6371a770245f989f67352a05d8bb6>:0 at System.Linq.Enumerable+WhereListIterator`1[TSource].MoveNext() [0x00037] in /private/tmp/source-mono-2017-02/bockbuild-2017-02/profiles/mono-mac-xamarin/build-root/mono-x86/external/corefx/src/System.Linq/src/System/Linq/Where.cs:369 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList 

同样的事情发生了,即。 URI http://127.0.0.1:8080/api/employee?id=3,但它的工作方式与URI http://127.0.0.1:8080/api/employee/3相同。它也按预期工作(从数据库中检索所有对象),以供URI http://127.0.0.1:8080/api/employee/使用。

唯一不工作的是参数。这是代码。

Controller.cs

[RoutePrefix("api/employee")] 
    public class EmployeeApiController : ApiController 
    { 

     readonly EmployeePersistence persistence; 


     public EmployeeApiController() 
     { 
      persistence = new EmployeePersistence(); 
     } 

     [HttpGet] 
     [Route("{id:long}")] 
     public IHttpActionResult GetEmployee(long id) 
     { 
      return Json(persistence.GetEmployee(id)); 
     } 

     [HttpGet] 
     [Route("")] 
     public IHttpActionResult ListEmployees([FromUri] EmployeeParameters parameters) 
     { 
      return Json(persistence.GetEmployeeList().Where(x => x.FirstName.Equals(parameters.FirstName))); 
     } 
    } 
} 

EmployeeParametes.cs

public class EmployeeParameters 
{ 
    /** 
    * First name of the employee. 
    */ 
    public string FirstName { get; set; } 

    /* 
    * Last name of the employee. 
    */ 
    public string LastName { get; set; } 

    /** 
    * Place of birth of the employee. 
    */ 
    public string BirthPlace { get; set; } 

    /** 
    * Gender of the employee. 
    */ 
    public int Gender { get; set; } 

    /** 
    * OIB of the employee. 
    */ 
    public string OIB { get; set; } 


    /** 
    * Current place of the employee. 
    */ 
    public string CurrentPlace { get; set; } 


    /** 
    * Department code of the employee. 
    */ 
    public string Department { get; set; } 

    public EmployeeParameters() 
    { 
    } 
} 

Persistence.cs

public List<Employee> GetEmployeeList() 
     { 
      string sqlString = "SELECT * FROM Employee"; 

      MySqlCommand cmd = new MySqlCommand(sqlString, conn); 

      List<Employee> employees = new List<Employee>(); 

      using (MySqlDataReader reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        employees.Add(ReadFromDatabase(reader)); 
       } 
      } 

      return employees; 
     } 

Employee ReadFromDatabase(MySqlDataReader dataReader) 
     { 
      if (!dataReader.Read()) 
      { 
       return null; 
      } 

      string firstName = null; 
      string lastName = null; 
      string birthPlace = null; 
      string currentPlace = null; 
      int gender = -1; 
      string departmentCode = null; 
      string OIB = null; 

      try 
      { 
       firstName = dataReader.GetString(1); 
       lastName = dataReader.GetString(2); 
       birthPlace = dataReader.GetString(3); 
       currentPlace = dataReader.GetString(4); 
       gender = dataReader.GetInt32(5); 
       departmentCode = dataReader.GetString(6); 
       OIB = dataReader.GetString(7); 
      } 
      catch (SqlNullValueException) 
      { 
       // log the error 
      } 

      return new Employee(
       firstName != null ? firstName : "N/A", 
       lastName != null ? lastName : "N/A", 
       birthPlace != null ? birthPlace : "N/A", 
       currentPlace != null ? currentPlace : "N/A", 
       gender != -1 ? (gender == 1 ? EmployeeGender.M : EmployeeGender.F) : 
        EmployeeGender.UNDEFINED, 
       departmentCode != null ? 
        DepartmentCodeExtensions.GetEnumValue(DepartmentCode.NONE, departmentCode) : 
        DepartmentCode.NONE, 
       OIB != null ? OIB : "N/A" 
      ); 
     } 

我猜测问题与路线有关,但不确定究竟是什么或如何解决它。任何帮助表示赞赏。

+0

请提供异常的完整堆栈跟踪以及三个.cs文件的完整内容 - 否则,很难判断出错的位置。 –

+0

@ J.N。根据要求更新。 – wesleyy

+0

您是否运行过调试器并验证它是否为参数对象,而不是Persistence类返回的'Employees'集合? – Eris

我认为这个例外的出现是因为在while -loop和方法ReadFromDatabase中都调用了.Read()。您应该删除后者的.Read()呼叫。

由于您的代码现在在读取器中提取数据之前,需要在读取器中执行两次.Read()调用。 另一个问题是,如果读者无法阅读更多内容,则将null放入您的员工列表中,并在尝试使用此元素的FirstName属性时保证NullPointerException