【发布时间】:2015-05-14 16:18:56
【问题描述】:
我正在尝试创建一个快速应用程序来尝试使用 Web API 学习 AngularJS(我使用过 ASP.NET MVC,但没有机会使用 Web API)服务器端,但我似乎无法让我的对象发布 Web API 方法时进行序列化。
我的对象是一个简单的用户对象,它继承自 BaseEntity 对象:
public class User : BaseEntity
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the email.
/// </summary>
/// <value>
/// The email.
/// </value>
public string Email { get; set; }
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>
/// The password.
/// </value>
public string Password { get; set; }
}
public class BaseEntity : MongoRepository.Entity, Interfaces.IEntity
{
/// <summary>
/// Gets or sets the id for this object (the primary record for an entity).
/// </summary>
/// <value>
/// The id for this object (the primary record for an entity).
/// </value>
public new int Id { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="BaseEntity"/> is selected.
/// </summary>
/// <value>
/// <c>true</c> if selected; otherwise, <c>false</c>.
/// </value>
public bool Selected { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="BaseEntity"/> is deleted.
/// </summary>
/// <value>
/// <c>true</c> if deleted; otherwise, <c>false</c>.
/// </value>
public bool Deleted { get; set; }
}
我最近的方法尝试如下:
public HttpResponseMessage Post(JObject user)
{
User userObject = user.ToObject<User>();
_Repo.Add(userObject);
return Request.CreateResponse(HttpStatusCode.Created, "User Created");
}
我的 AngularJS sn-p 以更清楚地了解从 UI 发布的内容(属性是使用 ng-model 设置的,我可以看到它们是通过 Fiddler 传递的,即使 Web API 方法将这些属性反序列化为null 而不是 ""):
$scope.User = {
Name: '',
Password: '',
Email: '',
}
$scope.register = function () {
$http({
method: 'POST',
url: '/api/User/',
data: $scope.User //Also tried JSON.stringify($scope.User)
})
.success(function () {
//Handle successful registration
})
.error(function () {
//Show error message
});
}
调试时,我可以看到用户参数已经填充了一个对象。我尝试在请求期间设置所有属性,只有一个属性等,并通过 POSTman 和应用程序调用了 api,无论我使用哪种方式,当我尝试将用户参数对象解析为我的用户对象时,我得到一个空白用户对象.当我将参数类型作为我的用户对象时,我也会得到相同的结果。
我环顾四周,大多数示例和回答的问题都没有继承自任何东西,并且我尝试了不同的 Content-Type 认为这可能是问题所在(这是当我拥有 User 对象类型和方法)。
当涉及到从其他类派生的对象时,JSON.NET/内置 Web API 反序列化是否缺少一个怪癖,或者我缺少什么?
编辑:我已经从我的对象中删除了所有继承,并且它按我的预期绑定,所以我不确定为什么当有任何类型的继承时它不起作用。当一个对象从另一个对象继承时,是否需要进行一些设置才能使参数绑定起作用?:
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
【问题讨论】:
标签: json angularjs asp.net-web-api