gpt4 book ai didi

c# - ASP.Net web api 后操作参数总是为空

转载 作者:行者123 更新时间:2023-11-30 17:52:58 25 4
gpt4 key购买 nike

在我的 asp.net web api 中,我的 post action 参数总是得到空值。

这是我的行为。

[System.Web.Mvc.HttpPost]
public HttpResponseMessage Add([FromBody]Products id)
{

var response = new HttpResponseMessage();
try
{
if (id.ProductsList.Length > 0)
{
response.StatusCode = HttpStatusCode.OK;
response.EnsureSuccessStatusCode();
response.Content = new StringContent(string.Format("Number of products {0}",id.ProductsList.Length) );
Logger.Info(string.Format("Number of products {0}", id.ProductsList.Length));
}
response.StatusCode = HttpStatusCode.BadRequest;
}
catch (Exception ex)
{
response.StatusCode = HttpStatusCode.InternalServerError;
response.Content = new StringContent("Error occured");
Logger.Error(ex);
}
return response;
}

这就是我尝试调用 API 的方式。

var filePath = @"C:\Apps\Eastworks\Lott\boots.xml";
var xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);

var client = new HttpClient();
MediaTypeFormatter jsonFormatter = new XmlMediaTypeFormatter();

HttpContent content = new ObjectContent<string>(xmlDoc.OuterXml, jsonFormatter);
if (content.Headers.Contains("Content-Type"))
{
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
}

var result = client.PostAsync("http://localhost:3103/Products/Add",
content)
.Result;

以下是我的模型。

[XmlRoot("products")]
public class Products
{
[XmlElement("product")]
public Product[] ProductsList { get; set; }
}

public class Product
{
public Product()
{
//default implementation

}
[XmlElement("code")]
public string Code { get; set; }

[XmlElement("related-product")]
public RelatedProduct[] RelatedProducts { get; set; }


[XmlElement("description")]
public string Description { get; set; }

// Removed some of the properties.

[XmlElement("variant")]
public Variant[] Variants { get; set; }
}

这是我的 xml。

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<code>mipacaloha</code>
<related-product>paisley_blk</related-product>
<related-product>mipacpolkadot</related-product>

<description>Classic MiPac silhouette. 30cm (12 inches) wide by 37cm (15 inches) high with a 15cm (6 inches) depth.</description>
<brand>Mi Pac</brand>
<style>Backpack</style>
<model-name>Pocket Prints</model-name>
<weight>0.4</weight>
<gender>womens</gender>

<variant>
<bag-details />
<exact-colour>Aloha Sky blue</exact-colour>
<colour>Blue</colour>
<pic-url>005872</pic-url>
<sku>136200</sku>
<ean>5053466362002</ean>
<stock>0</stock>
<price>21.99</price>
</variant>
<variant>
<bag-details />
<exact-colour>Aloha Purple</exact-colour>
<colour>Purple</colour>
<pic-url>mipacaloha</pic-url>
<sku>121521</sku>
<ean>5053466215216</ean>
<stock>6</stock>
<price>18.99</price>
<original-price>21.99</original-price>
</variant>

</product>
</products>

我将 XmlSerializer 设置为在 Application_Start 中使用。

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

我看过很多与我相似的帖子。但我无法解决我的问题。请多多指教。

谢谢,纳什

最佳答案

您是否指定 Web API 以使用 XmlSerializer?默认情况下,它使用 DataContractSerializer 来绑定(bind) XML 请求。

关于c# - ASP.Net web api 后操作参数总是为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17968483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com