1

I send JSON data from android app. In server side I have issue - Web API server controller doesn't get parametrs. From client side I send json data like this:

{   "TypeID ": "1",
    "FullName": "Alex",
    "Title": "AlexTitle",
    "RegionID": "1",
    "CityID ": "1",
    "Phone1": "+7(705)105-78-70"
}

Any help appreciated, please if you have some information, idea let me know, thank you! This is controller

 [System.Web.Http.HttpPost]
    public HttpResponseMessage PostRequisition([FromBody]string requisition)
    {
        Requisition postReq = new Requisition();
       if (!String.IsNullOrEmpty(requisition))
        {
            dynamic arr = JValue.Parse(requisition);
            //PostReq model = JsonConvert.DeserializeObject<PostReq>(requisition);
            postReq.FullName = arr.FullName;
            postReq.CityID = Convert.ToInt32(arr.CityID);
            postReq.RegionID = Convert.ToInt32(arr.RegionID);
            postReq.TypeID = Convert.ToInt32(arr.TypeID);
            postReq.UserID = 8;
            postReq.Title = arr.Title;
            postReq.Date = Convert.ToDateTime(arr.Date, CultureInfo.CurrentCulture);
            postReq.Decription = arr.Description;
            postReq.Phone1 = arr.Phone1;
            postReq.Activate = false;
            postReq.ClickCount = 0;
            try
            {
                db.Requisition.Add(postReq);
                db.SaveChanges();
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "OK";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, postReq);
            }
            catch (Exception ex)
            {
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "ERROR";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, ex.Message);
            }
        }
        else
        {
            Message msg = new Message();
            msg.Date = DateTime.Now;
            msg.Type = "POST";
            msg.Text = "null";
            db.Message.Add(msg);
            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK, "null");
        }

    }
3
  • this is not asp.net web api, its C# web api 2.0 Commented Feb 29, 2016 at 12:17
  • @Bhargav sorry but among tags I didn't find any suitable tag Commented Feb 29, 2016 at 12:20
  • yes I just searched its very weird that c# web api 2.0 is tagged as asp.net web api very weird Commented Feb 29, 2016 at 12:24

1 Answer 1

3

Your problem is simple. You are sending a JSON object but are expecting a string in your POST action. Simple way to fix this is creating a class that maps to your JSON object:

public class RequisitionViewModel
{
    public int TypeID {get; set;}
    public string FullName {get; set;}
    public string Title {get; set;}
    public int RegionID {get; set;}
    public int CityID  {get; set;}
    public string Phone1 {get; set;}    
}

Then, change your action signature to:

[FromBody]RequisitionViewModel requisition)

You also don't need all the converting in your code:

postReq.FullName = requisition.FullName;
postReq.CityID = requisition.CityID;
//other fields...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.