Reputation: 872
I am using ASP.NET WEB API.
I want to receive and add to database jsonstring.
My model:
namespace sms.Models
{
[JsonObject]
public class MySMS
{
//[JsonProperty("id")]
//public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("fromnumber")]
public string FromNumber { get; set; }
[JsonProperty("tonumber")]
public string ToNumber { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
}
SMSController:
public void Post(JObject singleSMS)
{
MySMS smska = singleSMS.ToObject<MySMS>();
try
{
SMS_Repository.Add(smska);
}
catch (Exception)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotImplemented));
}
}
and method from repository:
public static void Add(MySMS singleSMS)
{
DataClasses1DataContext db = new DataClasses1DataContext();
SimpleSMS newSMS = new SimpleSMS();
newSMS.Name = singleSMS.Name;
newSMS.Text = singleSMS.Text;
newSMS.FromNumber = singleSMS.FromNumber;
newSMS.ToNumber = singleSMS.ToNumber;
db.SimpleSMS.InsertOnSubmit(newSMS);
db.SubmitChanges();
}
Now, if i send POST request from Fiddler like this:
localhost:25856/api/SMS
with Request Body:
{"name":"name", "tonumber":"1", "fromnumber":"2", "text":"text"}
i have null reference exception over here:
MySMS smska = singleSMS.ToObject<MySMS>();
smska is null
.
What is my mistake?
Upvotes: 3
Views: 1115
Reputation: 4978
Try setting Content-Type: application/json in your Fiddler request.
Upvotes: 1