2016-08-11 36 views
0

今天我問你,因爲我很難找到適當的解決方案來解決這個特定的問題。從MVC Web客戶端(HttpWebResponse)向Web Api傳遞搜索參數(純文本/ json)

我有一個MVC客戶端應用程序,它通過HttpWebResponse與Web Api進行通信,該API用於檢索數據並操作結果。

由於所有請求都是在部分視圖中處理的,因此我會通過AJAX調用所有相關的函數/方法。

像這樣:

$('#dosearch').click(function() { 
    var searchstring = "{ "; 
    $('#tab_' + $("ul#tabs_searchmask_header li.active").attr('id')).find('input:text').each(function() { 
     var el = $(this); 
     if (el.val() !== "") { 
      searchstring += "\"" + el.attr('id') + "\": [ \"" + el.val() + "\" ], "; 
     } 
    }); 
    searchstring += "}"; 
    searchstring = searchstring.replace(" ], }", "] }"); 

    $.ajax({ 
     url: '@Url.Action("GetResults", "SearchResult", null, Request.Url?.Scheme)', 
     type: 'POST', 
     data: { filecabinet: '@Model.FileCabinet', search: searchstring } 
    }).done(function (partialViewResult) { 
      $("#_pviewcontent").html(partialViewResult); 
    }); 
}); 

這將創建一個包含所有相關數據,我需要一個簡單的JSON字符串。由於查詢大小隻能容納最多2000個字符,因此我必須使用'POST'來擴展可傳遞的數據/搜索參數的數量。

最終,這會調用控制器,獲取一些附加數據並將其傳遞給與Web Api控制器通信的包裝器。

var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); 
request.Credentials = CredentialCache.DefaultNetworkCredentials; 
request.Method = method; 
WebRequest.DefaultWebProxy = null; 
request.Proxy = WebRequest.DefaultWebProxy; 

if (method == WebRequestMethods.Http.Post) 
{ 
    data = "=" + data; 
    var bytes = new byte[data.Length * sizeof(char)]; 
    Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length); 
    request.ContentLength = bytes.Length; 
    request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; 
    request.Expect = "application/x-www-form-urlencoded; charset=UTF-8"; 
    request.GetRequestStream().Write(bytes, 0, bytes.Length); 
} 

return request.GetResponse() as HttpWebResponse; 

我試着改變內容類型爲「應用程序/ json」,但最終有一個null值作爲參數。

這是我的Web Api方法,它應該接收參數並將其保存在searchparams中。

[HttpPost] 
public ResultListModel GetResults(string sessionID, string filecabinet, [FromBody]string searchparams) 
{ 
    .... 
} 

我得到的只是一個字符串化的字節數組。我想知道,是否有另一種方法來傳遞數據:純文本(或至少是json對象)。

任何想法或建議嗎?

親切的問候

回答

0

通過簡單地改變

if (method == WebRequestMethods.Http.Post) 
{ 
    data = "=" + data; 
    var bytes = new byte[data.Length * sizeof(char)]; 
    Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length); 
    request.ContentLength = bytes.Length; 
    request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; 
    request.Expect = "application/x-www-form-urlencoded; charset=UTF-8"; 
    request.GetRequestStream().Write(bytes, 0, bytes.Length); 
} 

if (method == WebRequestMethods.Http.Post) 
{ 
    request.ContentType = "application/json"; 
    request.Accept = "application/json"; 
    request.Method = "POST"; 

    using (var streamWriter = new StreamWriter(request.GetRequestStream())) 
    { 
     streamWriter.Write(JObject.Parse(data)); 
     streamWriter.Flush(); 
    } 
} 

[HttpPost] 
public ResultListModel GetResults(string sessionID, string filecabinet, [FromBody]string searchparams) 
{ 
    .... 
} 

解決了這個謎
[HttpPost] 
public ResultListModel GetResults(string sessionID, string filecabinet, [FromBody]JObject searchparams) 
{ 
    .... 
} 

現在我的Web Api控制器接收一個包含所有需要的數據的json對象。