Reputation: 769
I am trying to perform an update on a resource using WebAPI
I have a controller which performs the Operation
AdminController class
[HttpPut]
[Route("newprofile")]
public HttpResponseMessage Update(AdminAction adminAction)
{
if (_adminManager.ApproveUser(adminAction))
{
return Request.CreateResponse(HttpStatusCode.OK);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
Resource
public class AdminAction : BaseResource
{
public bool Approve { get; set; }
public bool Delete { get; set; }
public string EmailId { get; set; }
public string Language { get; set; }
[IgnoreDataMember]
public Language eLanguage { get; set; }
}
Ajax code
activate: function (e) {
var email = this.model.get('Email');
var adminAction = {
"Approve": true,
"Delete": false,
"EmailId": email,
"Language": $("#ddlMotherTongue").val()
}
var url = kf.Settings.apiUrl() + '/admin/newprofile';
$.ajax(url, {
type: "put",
data: JSON.stringify(adminAction),
success: function (data, textStatus, jqXHR) {
alert('Profile approved');
$("table tr td").filter(function() {
return $(this).text().trim() == email;
}).parent('tr').css({'border': '1px solid green', 'border-left': 'none', 'border-right': 'none','color':'green'});
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 400) {
var errorJson = jQuery.parseJSON(jqXHR.responseText);
if (errorJson.hasOwnProperty("Errors")) {
switch (errorJson.Errors[0].ErrorType) {
}
}
}
},
async: true
});
return false;
},
Fiddler
URL: http://lapi.x.com/admin/newprofile
Request Headers
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Authorization: Token dd39c761-3060-498b-9393-92b12cace238
Connection: keep-alive
Content-Length: 76
Content-Type: application/json
Host: lapi.x.com
Origin: http://x.com
Referer: http://x.com/
Body
{"Approve":true,"Delete":false,"EmailId":"[email protected]","Language":"Hindi"}
When I execute the PUT Operation(Fiddler and Code) the admin object received in the put method(Code behind) has all the types in the object set to default values, i.e the passed values are not deserialized.
Any ideas about what I could have done to the JSON, that the server doesn't like it?
Upvotes: 1
Views: 579
Reputation: 12196
Because you're using DataContractJsonSerializer
you should have added the relevant attributes.
Class Attribute:
[DataContract]
Method Attribute:
[DataMember]
Upvotes: 1