我正在尝试将xml读入asp.net中的Web API。下面的代码片段显示了XML,类,webapiconfig和控制器类。我能够使用带有所有xml节点和属性的提琴手来读取响应。但是在使用cURL POST时,调试时控制器中的readresponse类对象变为null。我无法找出正确的cURL请求。
我的XML帖子请求:
<?xml version="1.0" encoding="UTF-8"?>
<Status xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ID="8038" Level="2030">
<Item>
<Product>Chocolate</Product>
<Category>Confectionary</Category>
<Price>100</Price>
<Results type="result" resultType="image">
<ProductLink>https://google.com</ProductLink>
</Results >
</Item>
</Status>
对应的班级:
[XmlRoot(ElementName="Results")]
public class Results {
[XmlElement(ElementName="ProductLink")]
public string ProductLink { get; set; }
[XmlAttribute(AttributeName="type")]
public string Type { get; set; }
[XmlAttribute(AttributeName="resultType")]
public string ResultType { get; set; }
}
[XmlRoot(ElementName="Item")]
public class Item {
[XmlElement(ElementName="Product")]
public string Product { get; set; }
[XmlElement(ElementName="Category")]
public string Category { get; set; }
[XmlElement(ElementName="Price")]
public string Price { get; set; }
[XmlElement(ElementName="Results")]
public Results Results { get; set; }
}
[XmlRoot(ElementName="Status")]
public class Status {
[XmlElement(ElementName="Item")]
public Item Item { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName="Level")]
public string Level { get; set; }
}
WebApiConfig
configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(New QueryStringMapping("type", "xml", New MediaTypeHeaderValue("application/xml")))
configuration.Formatters.XmlFormatter.UseXmlSerializer = True
configuration.Routes.MapHttpRoute(name:="DefaultApi", routeTemplate:="api/{controller}/{id}", defaults:=New With {Key .id = RouteParameter.[Optional]})
控制器类WEB API
Public Class MyAPIController
Inherits ApiController
Public Function Product(ByVal readresponse As Status) As String
End Function
End Class
cURL发布
curl -X POST -H "Content-type: application/xml" -d "<?xml version="1.0" encoding="UTF-8"?><Status xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ID="8038" Level="2030"><Item><Product>Chocolate</Product><Category>Confectionary</Category><Price>100</Price><Results type="result" resultType="image"><ProductLink>https://google.com</ProductLink></Results ></Item></Status>" http://localhost:10023/WebApp/api/MyAPI/Product