3

拥有这些 POCO:

public class UserRegistration
{
    [Required]
    public ApiKey ApiKey { get; set; }
    ...
}

public class ApiKey 
{
    [Required]
    public Guid AppKey { get; set; }
    ...
}

这个Json:

{
    "apiKey": {
      "appKey": "D8B4DE20-5654-43B0-B3F6-FDA416087FDE"
    }
}

我遇到了一个例外:

System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
at System.Web.Http.ApiController.d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()

将此称为 ASP.NET Web API 控制器:

public HttpResponseMessage Put([FromBody]UserRegistration userRegistration)
{
}

如果将属性类型从 Guid 更改为字符串,一切正常,但我想要 Guid。是否支持这样的场景?

4

1 回答 1

3

异常堆栈跟踪中没有明显遵循的问题不在于映射,而在于验证属性装饰:

[Required]
[StringLength(36)] // NO-NO!
public Guid AppKey { get; set; }
于 2013-07-23T20:47:38.843 回答