You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core中WordPressPCL 2.0.1的Post.CreateAsync调用失败求助

在ASP.NET Core中使用WordPressPCL 2.0.1创建文章时的反序列化错误问题

问题代码

var client = new WordPressClient( "http://sitioweb.com/wp-json/" );
client.Auth.UseBasicAuth( "username", "app_password" );
        
var post = new Post() {
  Title = new Title( "ITCmx" ),
  Content = new Content( "Content PostCreate" )
};

var createdPost = await client.Posts.CreateAsync( post );

错误信息

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'WordPressPCL.Models.Post' because the type requires a JSON object
(e.g. {"name":"value"}) to deserialize correctly. To fix this error
either change the JSON to a JSON object (e.g. {"name":"value"}) or
change the deserialized type to an array or a type that implements a
collection interface (e.g. ICollection, IList) like List that can be
deserialized from a JSON array. JsonArrayAttribute can also be added
to the type to force it to deserialize from a JSON array. Path '',
line 1, position 1.

解决方案

1. 修正WordPress API端点路径

WordPress REST API的文章操作端点默认在/wp/v2/posts,初始化WordPressClient时需包含完整基础路径,否则库可能请求错误端点(返回文章列表数组而非创建后的单个文章对象)。修改初始化代码:

var client = new WordPressClient( "http://sitioweb.com/wp-json/wp/v2/" );

2. 为Post对象添加必填状态字段

WordPress创建文章时需指定状态(默认可能未正确设置),添加Status属性确保API返回正确的单个文章对象:

var post = new Post() {
  Title = new Title( "ITCmx" ),
  Content = new Content( "Content PostCreate" ),
  Status = "publish" // 或 "draft",根据需求选择
};

3. 验证请求的实际端点与响应

通过调试工具(如Fiddler)或添加日志,确认CreateAsync调用的实际URL是否为http://sitioweb.com/wp-json/wp/v2/posts,并查看API返回的响应内容。若返回仍为数组,说明端点路径仍有问题,需检查WordPress站点的API配置。

4. 检查WordPressPCL版本兼容性

WordPressPCL 2.0.1可能存在与部分WordPress版本的兼容性问题,尝试更新到较新的稳定版本,或确认站点使用的WordPress版本是否支持该库的API调用规范。

内容的提问来源于stack exchange,提问作者Oscar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 12:45:37