使用 JSON 主体从 Fiddler 调用时,POST Web API 4 方法的参数为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14994062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:45:10  来源:igfitidea点击:

Parameter for POST Web API 4 method null when called from Fiddler with JSON body

jsonasp.net-mvc-4asp.net-web-apifiddlerc#-5.0

提问by Bill Sempf

I have a very simple Web API 4 controller over some legacy database code. The entity like like this:

我在一些遗留数据库代码上有一个非常简单的 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; }
}

The method looks like this:

该方法如下所示:

    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;
    }

When I POST through Fiddler like this:

当我像这样通过 Fiddler POST 时:

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":"[email protected]",
"jobCode":"GM",
"dateInJob":"8/7/2005",
"originalHire":"8/7/2005"
}

I get an exception from .NET and the item parameter is null.

我从 .NET 得到一个异常并且 item 参数为空。

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

What am I missing? I'm new to the Web API. Thanks in advance.

我错过了什么?我是 Web API 的新手。提前致谢。

回答by Calvin Allen

I think it is the request format in Fiddler. Try removing the quotes from the Content-Type header

我认为这是 Fiddler 中的请求格式。尝试从 Content-Type 标题中删除引号

From the Composer tab:

从作曲家选项卡:

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

Request Headers:

请求头:

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

Request Body:

请求正文:

{
   "employmentStatus":"zzz",
   "companyCode":"Titlemax",
   "division":"bbb",
   "orgLevel1Code":"ccc",
   "orgLevel2Code":"ddd",
   "orgLevel3":"eee",
   "storeName":"fff",
   "employeeNumber":"12343",
   "employeeFirstName":"Bill",
   "employeeMiddleInitial":"A",
   "employeeLastName":"sempf",
   "emailAddress":"[email protected]",
   "jobCode":"GM",
   "dateInJob":"8/7/2005",
   "originalHire":"8/7/2005"
}

Response:

回复:

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":"[email protected]","JobCode":"GM","DateInJob":"8/7/2005","OriginalHire":"8/7/2005"}

回答by Ronnel

Your fiddler request header should be like this:

您的提琴手请求标头应如下所示:

Content-Type: application/json

内容类型:应用程序/json