我正在使用Postman来测试WebApi方法,并且我遇到了XML问题。
我的WebApi2控制器使用以下put方法定义:
// PUT: api/DetailAttributes/5
[ResponseType(typeof(void))]
public IHttpActionResult PutDetailAttribute(int id, DetailAttribute detailAttribute)
{
// Processing code is here...
return StatusCode(HttpStatusCode.NoContent);
}
当我输出以下JSON时,一切都按预期工作:
{
"Id": 27,
"AttributeTypeId": 1,
"JobDetailId": 8,
"Name": "Attribute update from JSON Post",
"Value": "No error, just testing WebApi post for Attribute with JSON source.",
"ModifiedBy": "some person"
}
但是,当我PUT下面的XML时,AttributeTypeId属性被设置为0,即使我将其设置为1,如下所示:
<DetailAttribute xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DatafeedDashboard.Data">
<Id>27</Id>
<AttributeTypeId>1</AttributeTypeId>
<JobDetailId>8</JobDetailId>
<ModifiedBy>some person</ModifiedBy>
<Name>Attribute from XML Posttt</Name>
<Value>No error, just testing WebApi post for Attribute with XML source.</Value>
</DetailAttribute>
以下是Postman发送到我的网络服务器的确切消息(我也尝试将其设置为text / xml,但结果相同):
PUT /Dashboard/api/DetailAttributes/27 HTTP/1.1
Host: localhost:58130
Content-Type: application/xml
Cache-Control: no-cache
Postman-Token: 07172181-fc03-5696-189f-4178b5645384
<DetailAttribute xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DatafeedDashboard.Data">
<Id>27</Id>
<AttributeTypeId>1</AttributeTypeId>
<JobDetailId>8</JobDetailId>
<ModifiedBy>some person</ModifiedBy>
<Name>Attribute from XML Posttt</Name>
<Value>No error, just testing WebApi post for Attribute with XML source.</Value>
</DetailAttribute>
当我调试我的应用程序并检查传入的内容时,以下是我对JSON请求的看法:
以下是XML PUT请求的样子:
如您所见,它将值设置为0而不是1,这正是我所期望的。
另外,请注意如何正确地为JSON和XML传递JobDetailId的值,因此它似乎与int
数据类型无关。
以下是Entity Framework生成的DetailAttribute实体的代码:
public partial class DetailAttribute
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DetailAttribute()
{
this.CreatedBy = "";
this.ModifiedBy = "";
}
public int Id { get; set; }
public int AttributeTypeId { get; set; }
public int JobDetailId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public string CreatedBy { get; set; }
public System.DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }
public virtual AttributeType AttributeType { get; set; }
public virtual JobDetail JobDetail { get; set; }
}
有没有人知道为什么WebApi对PUT动词的处理方式不同?