14

私は、いくつかのレガシー データベース コードに対して非常に単純な 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 パラメーターが null です。

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException"}

私は何が欠けていますか?Web API は初めてです。前もって感謝します。

4

2 に答える 2

29

Fiddler のリクエスト形式だと思います。Content-Type ヘッダーから引用符を削除してみてください

コンポーザータブから:

POST http://localhost:1421/api/identity HTTP/1.1

リクエスト ヘッダー:

User-Agent: Fiddler
Host: localhost:1421
Content-Type: application/json; charset=utf-8

リクエスト本文:

{
   "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"
}

応答:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Location: http://localhost:1421/api/identity/12343
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcY2FsdmlfMDAwXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTJcUHJvamVjdHNcTXZjQXBwbGljYXRpb24yXE12Y0FwcGxpY2F0aW9uMlxhcGlcaWRlbnRpdHk=?=
X-Powered-By: ASP.NET
Date: Thu, 21 Feb 2013 03:53:04 GMT
Content-Length: 351

{"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"}
于 2013-02-21T03:58:29.010 に答える
3

フィドラー リクエスト ヘッダーは次のようになります。

コンテンツ タイプ: アプリケーション/json

于 2013-02-21T04:12:32.990 に答える