gpt4 book ai didi

c# - Web API 发布反序列化 json 结果但所有属性为空

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

几天来我一直在为这个问题绞尽脑汁。

使用 Angular $http 服务,我将回传到一个传递 Movie json 对象的 api Controller 。当正确的 Action 被命中时,所有的模型属性,原始的和复杂的,都是空的或默认的。我使用了 JsonProperty 属性,因为 json 对象属性的名称与我需要反序列化到的 c# 模型的名称不同。我也尝试过使用 DataContract,确保在应用程序启动时将 UseDataContractJsonSerializer 设置为 true,但这也没有效果。

我还应该注意,我使用的是 web api 2.2。

简化模型:

public class Movie {

[DatabaseGenerated(DatabaseGeneratedOption.None)]
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }

[JsonProperty(PropertyName = "title")]
public string Title { get; set; }

[JsonProperty(PropertyName = "year")]
public int Year { get; set; }

[JsonProperty(PropertyName = "release_dates")]
public ReleaseDates ReleaseDates { get; set; }

[JsonProperty(PropertyName = "ratings")]
public Ratings Ratings { get; set; }

[JsonProperty(PropertyName = "abridged_cast")]
public ICollection<Character> Cast { get; set; }

[JsonProperty(PropertyName = "users")]
public ICollection<User> Users { get; set; }
}

行动:

public async Task<HttpResponseMessage> Post(Movie movie) {
// do stuff
}

来自客户端的调用(在调试中我可以看到电影 js 对象与正确的数据混合):

$http.post('/api/usermovies', { movie : movie });

这是从角度发送的请求中的内容:

{
"movie" : {
"id" : "12897",
"title" : "The Matrix",
"year" : 1999,
"release_dates" : {
"theater" : "1999-03-31",
"dvd" : "1999-09-21"
},
"ratings" : {
"critics_rating" : "Certified Fresh",
"critics_score" : 87,
"audience_rating" : "Upright",
"audience_score" : 85
},
"abridged_cast" : [{
"name" : "Keanu Reeves",
"id" : "162654049",
"characters" : ["Neo"]
}, {
"name" : "Laurence Fishburne",
"id" : "162669090",
"characters" : ["Morpheus"]
}, {
"name" : "Carrie-Anne Moss",
"id" : "162669130",
"characters" : ["Trinity"]
}, {
"name" : "Hugo Weaving",
"id" : "162709905",
"characters" : ["Agent Smith"]
}, {
"name" : "Gloria Foster",
"id" : "364627698",
"characters" : ["The Oracle"]
}
],
"$$hashKey" : "00C"
}
}

追踪并没有真正发现任何东西。 modelBinder 似乎正确地将 json 与正确的模型相关联......

w3wp.exe Information: 0 : Request, Method=POST, Url=http://www.moovy.com/api/usermovies, Message='http://www.moovy.com/api/usermovies'

w3wp.exe Information: 0 : Message='UserMovies', Operation=DefaultHttpControllerSelector.SelectController

w3wp.exe Information: 0 : Message='Moovy.Api.Controllers.UserMoviesController', Operation=DefaultHttpControllerActivator.Create

w3wp.exe Information: 0 : Message='Moovy.Api.Controllers.UserMoviesController', Operation=HttpControllerDescriptor.CreateController

w3wp.exe Information: 0 : Message='Selected action 'Post(Movie movie)'', Operation=ApiControllerActionSelector.SelectAction

w3wp.exe Information: 0 : Message='Value read='Moovy.Models.Movie'', Operation=JsonMediaTypeFormatter.ReadFromStreamAsync

w3wp.exe Information: 0 : Message='Parameter 'movie' bound to the value 'Moovy.Models.Movie'', Operation=FormatterParameterBinding.ExecuteBindingAsync

w3wp.exe Information: 0 : Message='Model state is valid. Values: movie=Moovy.Models.Movie', Operation=HttpActionBinding.ExecuteBindingAsync

怎么办,怎么办?

最佳答案

我想通了。这是因为我在请求中传递电影对象的方式有误。

这...

$http.post('/api/usermovies', { movie : movie });

应该改成这样...

$http.post('/api/usermovies', movie);

从具有不同属性名称的 json 对象到 Movie 对象的反序列化在该更改后工作正常。

关于c# - Web API 发布反序列化 json 结果但所有属性为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24830396/

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