OK隨身攜帶,所以我有一個MediaTypeFormatter:的ASP.NET Web API PUT操作參數不通過
public class iOSDeviceXmlFormatter : BufferedMediaTypeFormatter
{
public iOSDeviceXmlFormatter() {
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
}
public override bool CanReadType(Type type) {
if (type == typeof(iOSDevice)) {
return true;
}
return false;
}
public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) {
iOSDevice device = null;
using (XmlReader reader = XmlReader.Create(readStream)) {
if (reader.ReadToFollowing("iOSDevice")) {
if (!reader.IsEmptyElement) {
device = new iOSDevice();
... do processing here ...
}
}
}
readStream.Close();
return device;
}
我有這樣的行動來處理PUT:
public void Put(string id, iOSDevice model)
{
}
我已經試過這是好:
public void Put(string id, [FromBody]iOSDevice model)
{
}
我已經試過這樣:
public void Put(string id, [FromBody]string value)
{
}
沒有這些工作時,我這樣做:
string data = "<iOSDevice>xml_goes_here</iOSDevice>";
WebClient client = new WebClient();
client.UploadString(url, "PUT", data);
行動拒絕觸發我iOSDeviceXmlFormatter,它甚至不會讀它作爲[FromBody]字符串。你如何得到這個東西來映射?
感謝,
佳佳
是的,這就是它!謝謝! –