2012-09-02 47 views
0

我有方法WCF像如何接受JSON字符串作爲參數

空隙最終化(串jsonstring);

我用一樣的finalize/{JSON}請求URL

但如果itried與JSON稱之爲它會給像charachters的壞請求原因:等

如何解決

這是我使用的方法

 [WebInvoke(Method = "POST", 
ResponseFormat = WebMessageFormat.Json, 
RequestFormat = WebMessageFormat.Json, 
UriTemplate = "FinalBooking/{BookingJsonString}")] 
     public int Finalize(string JsonString) 
     { 
      int result; // 1 success , 0 faill 

      JavaScriptSerializer json_serializer = new JavaScriptSerializer(); 
      Routess routes = 
        (Routess)json_serializer.DeserializeObject(JsonString); 
      using (XEntities context = new XEntities()) 
      { 
       result = context.usp_Final(routes.TypeID, routes.regID, routes.CycleID, routes.DateTime, 
         routes.LocationID, routes.PatID, routes.PO, routes.BTID, routes.DID); 



      } 

      return result; 

     } 

    } 

和示例中的數據的代碼簡單JSON字符串等

{"DateTime":"03/09/2012 09:00","CycleID":6,"BTID":31,"DiseaseID":814,"LocID":36,"PatID":13,"PO":1,"TypeID":744,"rID":-1} 

問候

+0

你確定':'是原因嗎? ':'在請求中是合法的。你可以發佈一些代碼嗎?和你正在傳遞的json字符串? – DarthVader

+0

你還使用什麼綁定? http? – DarthVader

+0

如果你在你的方法中設置了斷點,它會進入嗎?它是否將JSON字符串傳遞到您的方法中,並且Json序列化器會拋出錯誤,還是WCF拋出錯誤? – CodingWithSpike

回答

0

你URL編碼的JSON字符串,當你將它添加到你的網址是什麼?如果URL是從C#創建的,那麼您將使用類似HttpServerUtility.UrlEncode()或javascript的文本,escape()

如果您可以顯示正在發送的URL,那將會有所幫助。它不清楚URL是否是問題,或WCF中的東西。


編輯:

看您發佈的代碼,我注意到你的UriTemplate和你的方法參數名不匹配:

[WebInvoke(Method = "POST", 
ResponseFormat = WebMessageFormat.Json, 
RequestFormat = WebMessageFormat.Json, 
UriTemplate = "FinalBooking/{BookingJsonString}")] 
    public int Finalize(string JsonString) 

BookingJsonStringJsonString

UriTemplate有兩種模式的綁定:「名稱綁定」,其中名稱應匹配,「綁定位置」,它忽略名稱,只是通過參數索引。我似乎無法找到關於WCF WebInvoke使用哪種模式的任何信息,因此您可能想嘗試重命名模板或參數以匹配。

+0

檢查我更新的問題 – Ali

+0

更新了我的答案。 – CodingWithSpike

+0

好的我修復了它,但這不是問題 – Ali

相關問題