繁体   English   中英

无法将数据传递到Web方法始终获取空C#

[英]Unable to pass data to web method always getting null c#

我无法将数据从客户端应用程序发送到Web方法,实际上我不知道自己在做什么错,任何帮助将不胜感激。

客户:

    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
}

我想念什么? 提前致谢 !

使用Model接受Http Post,尽量不要使用字符串,例如

public class ReceiveModel
{
string Customer;
}

public ActionResult syncClientFromChild(ReceiveModel model)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM