简体   繁体   English

将自定义对象发送到asp.net Web服务

[英]Sending Custom Objects to an asp.net webservice

So I want to send a custom object to an asp.net 4.0 Webservice. 所以我想将自定义对象发送到asp.net 4.0 Web服务。
I have been sending a bunch of individual parameters but i want to start reusing some code so I'd like to do something like 我一直在发送一堆单独的参数,但是我想开始重用一些代码,所以我想做些类似的事情

    [WebMethod(Description = "Testing Sending Object In")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool COT(CodeWithMessage CWM)
    {
...

where the class for CodeWithMessage is CodeWithMessage的类在哪里

namespace UserSite
{
    namespace General
    {
        public class CodeWithMessage
        {
            public int iCode { get; set; }
            public string sMessage { get; set; }
            public CodeWithMessage()
            {
            }
        }
    }
}

The webservice defines the input as Web服务将输入定义为

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <COT xmlns="http://localhost">
          <CWM>
            <iCode>int</iCode>
            <sMessage>string</sMessage>
          </CWM>
        </COT>
      </soap:Body>
    </soap:Envelope>

I tried passing the webservice the following json 我尝试将以下json传递给webservice

      var postdata = { "iCode": -5, "sMessage": "BlogName " }

I get back an Internal Server Error [500] 我返回内部服务器错误[500]

{"Message":"Invalid JSON primitive: iCode." {“ Message”:“无效的JSON原语:iCode。”

Do I need to wrap this up somehow to indicate it is part of this object? 我是否需要以某种方式将其包装起来以表明它是该对象的一部分? I tried 我试过了

{"__type":"UserSite.General.CodeWithMessage","iCode":-5,"sMessage":"Blog Name Already Exists"}

Which is what CodeWithMessage returns from another webservice that sends it instead of receiving it. 这是CodeWithMessage从另一个发送而不是接收它的Web服务返回的。

Your error says that you Json is wrong you need to check the json string you are passing to the webservice. 您的错误表明您Json是错误的,您需要检查传递给Web服务的json字符串。

its may be like 它可能像

var postdata = { 'iCode': '-5', 'sMessage': 'BlogName' }

check the jSon String example : http://www.json.org/js.html on jSon site........ 在jSon网站上查看jSon字符串示例: http ://www.json.org/js.html。

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

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