2015-03-03 25 views
2

我能夠從我的角度控制器發送原始json對象,該角色控制器在我的web api方法中被反序列化爲已知類型。這很好,但我現在需要能夠在同一請求中發送其他參數,這些參數可以是json對象或簡單類型(如string或int)。將多個對象從角度控制器發佈到Web API 2

我見過諸如this這樣的文章,它們描述了我的問題,但他們是從代碼隱藏而不是客戶端發送他們的請求。

我試圖構造一個json數組併發送它,但是我收到以下消息:'無法創建抽象類'

控制器代碼(改編)

var request = { 
      params:[] 
     }; 

     request.params.push(jsonObjectA); 
     request.params.push({"someString" : "ABCDEF"}); 

     $http({ 
      method: 'POST', 
      url: targetUri, 
      data: request 
     }) 

的Web API方法簽名

public JsonResult TestMethod(JArray request) 

這是否進場聲明智?如果可以的話,我真的想避免爲每個請求創建dto對象。

回答

5

確定設法通過以下this article工作。

然後我能在我的API方法簽名使用簡單和複雜類型:

public JsonResult MyAction(StonglyTypedObject myObj, string description) 

,我通過在數據中的值參數:

$http({ 
      method: 'POST', 
      url: targetUri, 
      data: { 
       myObj: myObj, 
       description: "desc here" 
      } 
     }).success(... 

這是乾淨的解決方案,我可以找到,希望它也可以幫助你。

0
// Simple POST request example (passing data) : 
$http.post('/someUrl', {msg:'hello word!'}). 
    success(function(data, status, headers, config) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }). 
    error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

在POST方法第一個參數是URL和第二個是對象,ü希望u能在這個對象傳遞什麼都....

這可能是ü幫助..

+2

謝謝,但我也知道如何找到角度文檔:https://docs.angularjs.org/api/ng/service/$http 另外,請在將來更仔細地閱讀這個問題,我問的是如何我可以將2個參數添加到請求中,而不是您在發佈的代碼段中顯示的參數。 – Mike 2015-03-03 10:54:11

相關問題