2017-07-08 52 views
0

我想根據從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

回答

0

我認爲這個例外的出現是因爲在while -loop和方法ReadFromDatabase中都調用了.Read()。您應該刪除後者的.Read()呼叫。

由於您的代碼現在在讀取器中提取數據之前,需要在讀取器中執行兩次.Read()調用。 另一個問題是,如果讀者無法閱讀更多內容,則將null放入您的員工列表中,並在嘗試使用此元素的FirstName屬性時保證NullPointerException