我正在创建一个Web Api方法,该方法应该通过XML或JSON接受对象列表并将它们添加到数据库中.
这是我目前拥有的非常基本的版本:
[HttpPost]
public HttpResponseMessage Put([FromBody]ProductAdd productAdd)
{
//do stuff with productadd object
return Request.CreateResponse(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
它接受的对象列表的模型结构如下:
public class ProductAdd
{
public List<ProductInformation> Products { get; set; }
}
public class ProductInformation
{
public string ProductName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用XML时,上面的工作非常完美 - (Content-Type:application/xml)
<?xml version="1.0" encoding="utf-8"?>
<ProductAdd>
<Products>
<ProductInformation>
<ProductName>Seahorse Necklace</ProductName>
</ProductInformation>
</Products>
<Products>
<ProductInformation>
<ProductName>Ping Pong Necklace</ProductName>
</ProductInformation>
</Products>
</ProductAdd>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用JSON(Content-Type:application/json)提供相同的东西时,Products列表为空
{
"ProductAdd": {
"Products": [
{
"ProductInformation": { "ProductName": "Seahorse Necklace" }
},
{
"ProductInformation": { …Run Code Online (Sandbox Code Playgroud) Quick Cross Browser JS问题,在设置文本框的值时:
document.getElementById("balanceText").innerText = "111";
Run Code Online (Sandbox Code Playgroud)
和
document.getElementById("balanceText").value = "111";
Run Code Online (Sandbox Code Playgroud)
两者都在IE中工作,
但它们不适用于Chrome,FF,Opera或Safari.
是否有一种替代方法可以在这些浏览器中使用?
:ri我正在开发一个 .Net web api,我们有一个基类来处理来自我们 api 的所有响应。此类将始终作为请求的根返回,其中包含用户请求的任何数据。
因此,用户将始终收到以下内容的响应:
<Content xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.com/schemas/TestNamespace/Interface6/Schema.xsd">
<Item>Information Here</Item>
</Content>
Run Code Online (Sandbox Code Playgroud)
我有以下代码返回上述罚款:
[XmlRoot(ElementName = "Content", Namespace = "http://www.example.com/schemas/TestNamespace/Interface6/Schema.xsd")]
public class MyResponse<T> : IMyResponse<T>
where T : class
{//rest of class}
Run Code Online (Sandbox Code Playgroud)
所以无论返回数据的根标签是什么,它都会被改成“Content”。因此,如果我的代码中的 T 是 PersonList,它会在 XML 中更改为“内容”。这是为了在我们的回复中提供一致性。
现在我需要为标签添加一个前缀。“ri:”所以收到的回复将是:
<ri:Content xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ri="http://www.example.com/schemas/TestNamespace/Interface6/Schema.xsd">
<Item>Information Here</Item>
</ri:Content>
Run Code Online (Sandbox Code Playgroud)
每个接近我需要的问题都提供了在代码中添加前缀的解决方案。
我想知道是否有办法使用属性来做到这一点?
编辑:将 ri: 添加到 XmlRoot 属性的 ElementName 不起作用。
[XmlRoot(ElementName = "ri:Content", Namespace = "http://www.example.com/schemas/TestNamespace/Interface6/Schema.xsd")]
Run Code Online (Sandbox Code Playgroud)
返回为:
<ri_x003A_Content xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.com/schemas/TestNamespace/Interface6/Schema.xsd">
<Item>Information Here</Item>
</ri_x003A_Content>
Run Code Online (Sandbox Code Playgroud)