无法将数据传递给Web方法始终获取null c#

时间:2017-06-08 01:52:44

标签: c# asp.net-mvc web-services

我无法将数据从我的客户端应用程序发送到网络方法,我实际上并不知道我做错了任何帮助都会感激。

客户端:

    public void syncClient(Customer c) 
    {
        try
        {
                var datos = new JavaScriptSerializer().Serialize(c);
                DataContractJsonSerializer ser =
                new DataContractJsonSerializer(typeof(string));
                MemoryStream mem = new MemoryStream();
                ser.WriteObject(mem, datos);

                string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

                string url = store.ParentUrl + "Api/syncClientFromChild";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = @"application/json"; //set the content type to JSON
                request.Method = "POST";

                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

                byte[] send = encoding.GetBytes(data);
                request.ContentLength = send.Length;

                using (Stream newStream = request.GetRequestStream())
                {
                    newStream.Write(send, 0, send.Length);
                }

                WebResponse ws1 = request.GetResponse();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                ApiCtrlResponse response = serializer.Deserialize<ApiCtrlResponse>(serializer.Serialize(ws1.GetResponseStream()));
                if (response.success == true)
                {
                    c.Synced = true;
                    _customerService.UpdateCustomer(c);
                }

        }
        catch (Exception A)
        {
        }
    }

我相信这很好,但另一种方法我总是在我需要接收的字符串中得到null:

[System.Web.Services.WebMethod]
public ActionResult syncClientFromChild(string Customer)
{
    // do something
}

我缺少什么?提前谢谢!

1 个答案:

答案 0 :(得分:0)

使用模型接受Http Post,尝试不使用字符串,如

public class ReceiveModel
{
string Customer;
}

public ActionResult syncClientFromChild(ReceiveModel model)