none
Object show always null when i post an object from asp.net core rc2 mvc controller to web api controller

    Question

  • Here is my code :

    calling web api from MVC controller                              

     using(HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:61810/api/Grid/GetConfiguration");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                    HttpContent content = new StringContent(JsonConvert.SerializeObject(objControlConfig), Encoding.UTF8, "application/json");
                    HttpResponseMessage responseMessage = client.PostAsync(new Uri("http://localhost:61810/api/Grid/GetConfiguration"), content).Result;

                    var message = responseMessage.Content.ReadAsStringAsync().Result;
                }

    web api call:

    -------------------

     [HttpPost]
            public JsonResult GetConfiguration([FromBody]ControlConfig objControlConfig)
            {
                string result = string.Empty;
                try
                {
                    using (SqlConnection con = new SqlConnection(strCon))
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("BWCC_SPGETCONFIG", con);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Category", objControlConfig.Category);
                        cmd.Parameters.AddWithValue("@SubCategory", objControlConfig.SubCategory);
                        result = Convert.ToString(cmd.ExecuteScalar());
                        con.Close();
                    }
                }
                catch (Exception e)
                {

                }
                if (result != null)
                    return Json(result);
                else
                    return null;
            }

    this "[FromBody]ControlConfig objControlConfig" is always getting null .........tried different ways but no luck.

    could you plz help anyone.

    Thanks!

    Anitha.


    Tuesday, June 14, 2016 12:09 PM

Answers

  • I'd ask in microsoft official ASP.Net forums here.

    http://forums.asp.net/1146.aspx/1?MVC

     

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Tuesday, June 14, 2016 12:37 PM
    Moderator