2
有theese波蘇斯:InvalidCastException的映射JSON字符串到GUID屬性
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。是否支持這種情況?
這應該是有幫助的http://stackoverflow.com/questions/13640463/json- net-exception-when-deserializing -a-guid-array-of-guids – Ehsan
@EhsanUllah:這是否意味着Guid不受支持,我需要接受字符串然後手動解析它? – abatishchev
非常如此。 – Ehsan