Ich habe einen Web-API-Service ursprünglich mit Beta-Bits, die ich mit den Release-Kandidaten-Bits umgebaut habe und ich habe jetzt dieses Problem.AspNet WebApi POST-Parameter ist Null beim Senden von XML
Ich habe eine POST-Aktion, die eine komplexe Option als einzigen Parameter verwendet. Wenn ich die Anfrage mit dem Body im JSON-Format sende, wird das Objekt wie erwartet deserialisiert, aber wenn ich stattdessen XML sende, ist der Parameter null.
In der Beta-I-Bindung durch die Deaktivierung Modell um das funktionierte wie in Carlos Figueira Blogbeitrag beschrieben Disabling model binding on ASP.NET Web APIs Beta
Im RC aber sie IRequestContentReadPolicy entfernt haben, dass diese Methode wurde implementiert.
Meine Aktion:
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;
}
}
Mein Modell:
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; }
}
Ich teste Fiddler mit Anfragen an den Dienst zu senden.
Dies funktioniert und gibt mir die erwarteten Ergebnisse zurück.
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"
}
]
}
Dies schlägt fehl, da der requestValues Parameter
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>
Details sehen in dieser [Frage] [1] ... [1]: http://stackoverflow.com/questions/14068288/model-always-null-on-xml-post – barrypicker