发送XML时,AspNet WebApi POST参数为null

我有一个最初使用beta位的web api服务,我已经使用候选版本位进行了重建,现在我遇到了这个问题。

我有一个POST操作,它将复杂选项作为唯一参数。 当我以json格式发送请求时,对象按预期反序列化,但如果我发送XML而不是参数为null。

在测试版中,我通过禁用模型绑定来解决这个问题,如Carlos Figueira的博客文章中描述的禁用ASP.NET Web API Beta上的模型绑定

但是在RC中,他们已经删除了此方法正在实现的IRequestContentReadPolicy。

我的行动:

public List Post([FromBody]Models.AimiRequest requestValues) { try { if (requestValues == null) { var errorResponse = new HttpResponseMessage(); errorResponse.StatusCode = HttpStatusCode.NotFound; errorResponse.Content = new StringContent("parameter 'request' is null"); throw new HttpResponseException(errorResponse); } var metadataParams = new List<KeyValuePair>(); foreach (Models.MetadataQueryParameter param in requestValues.Metadata) { metadataParams.Add(new KeyValuePair(param.Name, param.Value)); } List data = _payloadService.FindPayloads(metadataParams, requestValues.ContentType, requestValues.RuleTypes); var retVal = AutoMapper.Mapper.Map<List, List>(data); return retVal; // new HttpResponseMessage<List>(retVal); } catch (System.Exception ex) { _logger.RaiseError(ex); throw; } } 

我的模特:

 public class AimiRequest { public MetadataQueryParameter[] Metadata { get; set; } public string ContentType { get; set; } public string RuleTypes { get; set; } } public class MetadataQueryParameter { public string Name { get; set; } public string Value { get; set; } } 

我正在测试使用Fiddler向服务发送请求。

这有效,并返回预期的结果。

 POST http://localhost:51657/api/search HTTP/1.1 User-Agent: Fiddler Content-Type: application/json; charset=utf-8 Accept: application/json Host: localhost:51657 Content-Length: 219 { "ContentType":null, "RuleTypes":null, "Metadata":[ { "Name":"ClientName", "Value":"Client One" }, { "Name":"ClientName", "Value":"Client Two" } ] } 

这会失败,因为requestValues参数为null

 POST http://localhost:51657/api/search HTTP/1.1 User-Agent: Fiddler Content-Type: application/xml; charset=utf-8 Accept: application/xml Host: localhost:51657 Content-Length: 213      ClientName Client One   ClientName Client Two    

通过将以下行添加到Global.asax.cs中的ApplicationStart()方法,原始XML请求应该起作用:

 var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; xml.UseXmlSerializer = true; 

默认情况下,Web API使用DataContractSerializer类,这需要XML请求中的额外信息。

XmlSerializer对我来说似乎更顺利,因为我不必将模型的namepsace添加到每个XML请求。

我再一次在Mike Wasson的文章中找到了我的信息: http : //www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml_media_type_formatter

我遇到了和你一样的问题,并通过使用XmlMediaTypeFormatter序列化对象找到了解决方案,如下所述: http : //www.asp.net/web-api/overview/formats-and-model-binding/json-和-xml-serialization#testing_object_serialization 。 我使用了本文底部的“测试对象序列化”部分中的代码,并用我的模型替换了他的Person对象。

当我序列化我的对象时,我注意到以下属性被添加到根节点:

 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/NAMESPACE.OF.YOUR.MODEL" 

如果像这样将这些属性添加到xml中,则控制器应该正确序列化requestValues对象:

      ClientName Client One   ClientName Client Two