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

Nic*_*ick 24 c# asp.net-web-api

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

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

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

然而,在RC中,他们已经删除了此方法正在实现的IRequestContentReadPolicy.

我的行动:

public List<Models.Payload> 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<string, string>>();
    foreach (Models.MetadataQueryParameter param in requestValues.Metadata)
    {
      metadataParams.Add(new KeyValuePair<string, string>(param.Name, param.Value));
    }
    List<Core.Data.Payload> data = _payloadService.FindPayloads(metadataParams, requestValues.ContentType, requestValues.RuleTypes);
    var retVal = AutoMapper.Mapper.Map<List<Core.Data.Payload>, List<Models.Payload>>(data);
    return retVal; // new HttpResponseMessage<List<Models.Payload>>(retVal);
  }
  catch (System.Exception ex)
  {
    _logger.RaiseError(ex);
    throw;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的模特:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我正在测试使用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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这会失败,因为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

<AimiRequest>
  <ContentType />
  <RuleTypes />
  <Metadata>
    <MetadataQueryParameter>
      <Name>ClientName</Name>
      <Value>Client One</Value>
    </MetadataQueryParameter>
    <MetadataQueryParameter>
      <Name>ClientName</Name>
      <Value>Client Two</Value>
    </MetadataQueryParameter>
  </Metadata>
</AimiRequest>
Run Code Online (Sandbox Code Playgroud)

mat*_*404 26

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

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
Run Code Online (Sandbox Code Playgroud)

默认情况下,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


mat*_*404 5

我遇到了和你一样的问题,并通过使用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"
Run Code Online (Sandbox Code Playgroud)

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

<AimiRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://schemas.datacontract.org/2004/07/NAMESPACE.OF.YOUR.MODEL">
  <ContentType />
  <RuleTypes />
  <Metadata>
    <MetadataQueryParameter>
      <Name>ClientName</Name>
      <Value>Client One</Value>
    </MetadataQueryParameter>
    <MetadataQueryParameter>
      <Name>ClientName</Name>
      <Value>Client Two</Value>
    </MetadataQueryParameter>
  </Metadata>
</AimiRequest>
Run Code Online (Sandbox Code Playgroud)