在一些遗留数据库代码上,我有一个非常简单的 Web API 4 控制器。像这样的实体:
public class Employee
{
public string EmploymentStatus { get; set; }
public string CompanyCode { get; set; }
public string Division { get; set; }
public string OrgLevel1Code { get; set; }
public string OrgLevel2Code { get; set; }
public string OrgLevel3 { get; set; }
public string StoreName { get; set; }
public string EmployeeNumber { get; set; }
public string EmployeeFirstName { get; set; }
public string EmployeeMiddleInitial { get; set; }
public string EmployeeLastName { get; set; }
public string EmailAddress { get; set; }
public string JobCode { get; set; }
public string DateInJob { get; set; }
public string OriginalHire { get; set; }
}
该方法如下所示:
public HttpResponseMessage PostEmployee(Employee item)
{
DataHelpers.AddUser(item.CompanyCode, item.Division, item.OrgLevel1Code, item.OrgLevel2Code, item.OrgLevel3, item.EmployeeFirstName, item.EmployeeMiddleInitial, item.EmployeeLastName, item.EmailAddress, item.JobCode, item.OriginalHire);
var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.EmployeeNumber });
response.Headers.Location = new Uri(uri);
return response;
}
当我像这样通过 Fiddler 发布时:
POST /api/identity HTTP/1.1
User-Agent: Fiddler
Host: localhost:1421
Content-Length: 382
contentType: "application/json; charset=utf-8"
dataType: 'json'
{
"employmentStatus":"zzz",
"companyCode":"Titlemax",
"division":"bbb",
"orgLevel1Code":"ccc",
"orgLevel2Code":"ddd",
"orgLevel3":"eee",
"storeName":"fff",
"employeeNumber":"12343",
"employeeFirstName":"Bill",
"employeeMiddleInitial":"A",
"employeeLastName":"sempf",
"emailAddress":"bill@sempf.net",
"jobCode":"GM",
"dateInJob":"8/7/2005",
"originalHire":"8/7/2005"
}
我从 .NET 中得到一个异常,并且 item 参数为空。
{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException"}
我错过了什么?我是 Web API 的新手。提前致谢。