I am trying to post the data from jquery to ASP.Net WebAPI post method in order to save it.
here is my jquery method that sends the request
'saveAbsenceRecord': function (absenceRecord) {
var url = helper.apiPath + 'absence';
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(absenceRecord) ,
contentType: 'application/json; charset=utf-8',
async: false,
success: function (data) {
return data;
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
};
Below is my json as captured from request
"{\"AbsenceDate\":\"11 March 2014\",\"IsHalfDay\":false,\"DateModified\":\"18 March 2014\",\"User\":{\"Id\":2,\"Name\":\"\",\"EmpId\":\"\",\"LanId\":\"\",\"EmailId\":\"\",\"Permissions\":[]},\"ModifiedBy\":{\"Id\":2,\"Name\":\"\",\"EmpId\":\"\",\"LanId\":\"\",\"EmailId\":\"\",\"Permissions\":[]},\"Type\":{\"Id\":4,\"Reason\":\"Unplanned\"},\"Description\":\"Test\"}"
Here is my WebAPI post method
// POST api/absence
public HttpResponseMessage Post([FromBody]AbsenceRecord absenceRecord)
{
try
{
if (ModelState.IsValid)
{
Store store = new Store();
var result = store.UpdateAbsenceRecord(absenceRecord);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Invalid model state");
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
Here is how my parameter type
public class User : IEntity
{
public int Id { get; set; }
[MaxLength(10)]
public string EmpId { get; set; }
[MaxLength(50)]
public string UserName { get; set; }
public virtual List<UserPermission> Permissions { get; set; }
public string EmailId { get; set; }
public string LanId { get; set; }
}
public class AbsenceType : IEntity
{
public int Id { get; set; }
[MaxLength(50)]
public string Reason { get; set; }
[MaxLength(10)]
public string ColorHash { get; set; }
}
public class AbsenceRecord : IEntity
{
public int Id { get; set; }
public DateTime AbsenceDate { get; set; }
public virtual AbsenceType Type { get; set; }
public bool IsHalfDay { get; set; }
public DateTime DateModified { get; set; }
public string Description { get; set; }
public virtual User User { get; set; }
public virtual User ModifiedBy { get; set; }
}
I have a doubt that my Json isn't matching to the structure of the parameter type. So I would need eiter model binder or MediaType Formatter. Since I want to send the data in body I would not opt for Binder.
I have tried writting media type formatter and registered it as
public class AbsenceRecordFormatter : MediaTypeFormatter
{
public AbsenceRecordFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
}
public override bool CanReadType(Type type)
{
return type == typeof(AbsenceRecord);
}
public override bool CanWriteType(Type type)
{
return type == typeof(AbsenceRecord);
}
public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
{
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
config.Formatters.Add(new AbsenceRecordFormatter());
but that code isn't invoked when this method is called.
Can any body suggested why formatter is not being invoked please?