簡體   English   中英

將自定義對象發送到asp.net Web服務

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

所以我想將自定義對象發送到asp.net 4.0 Web服務。
我一直在發送一堆單獨的參數,但是我想開始重用一些代碼,所以我想做些類似的事情

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

CodeWithMessage的類在哪里

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

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>

我嘗試將以下json傳遞給webservice

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

我返回內部服務器錯誤[500]

{“ Message”:“無效的JSON原語:iCode。”

我是否需要以某種方式將其包裝起來以表明它是該對象的一部分? 我試過了

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

這是CodeWithMessage從另一個發送而不是接收它的Web服務返回的。

您的錯誤表明您Json是錯誤的,您需要檢查傳遞給Web服務的json字符串。

它可能像

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

在jSon網站上查看jSon字符串示例: http ://www.json.org/js.html。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM