我有一个带有控制器方法的web api:
[Route("{player}/title(gameA)/appearance")]
public async Task<string> PutAppearance([ModelBinder]Player player, [FromBody] PlayerAppearance appearance){ //...stuff}
玩家外观如下所示:
public class PlayerAppearance : PlayerAppearanceBase {///stuff}
和PlayerBase
public abstract class PlayerAppearanceBase : IPlayerAppearance
{
[Required]
public string PlayerId { get; set; }
public string TitleId{ get; set; }
当我发送一个请求时,请提出适当的请求:
{"PlayerId":"123456","LastModified":"0001-01-01T00:00:00","TitleId":"gameA"}
我收到以下错误:
"ModelState":{"appearance.PlayerId":["The PlayerId field is required."]}
现在我将控制器更改为如下所示:
public async Task<string> PutAppearance([ModelBinder]Player player)
{
var appearance =
JsonConvert.DeserializeObject<PlayerAppearance>(Request.Content.ReadAsStringAsync().Result);
///...}
一切正常。为什么它在简单的参数绑定方面会出现问题,但在直接反序列化时它可以正常工作。我失踪的幕后发生了什么?
*请注意,我已经从我的PlayerAppearance类中删除了无关的值