3

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?

1
  • But what I must to do if I need to receive collection of smses?? Commented Jun 18, 2013 at 10:37

1 Answer 1

1

Try setting Content-Type: application/json in your Fiddler request.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.