簡體   English   中英

Asp.Net WebApi,REST控制器和字節數組反序列化

[英]Asp.Net WebApi, REST controller and byte array deserialization

我已經實現了這樣的控制器:

[HttpPost]
[ActionName("DefaultAction")]
public HttpResponseMessage Post(Person obj){
}

人是這個階層:

public class Person
{
   public String Name { get; set; }
   public byte[] ImageStream { get; set; }
}

在客戶端,我發出此呼叫以發布新人員:

var person={
            "Name":"Test",
            "ImageStream":"AQID"
}

$.ajax({
            type: "POST",
            url: "/person",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(person),
            success: function (result) {
                console.log(result); //log to the console to see whether it worked
            },
            error: function (error) {
                alert("There was an error posting the data to the server: " + error.responseText);
            }
});

問題是我收到ImageStream設置為null的Person obj。

要測試我使用的ImageStream字符串是否正確,我嘗試了以下操作:

Person p=new Person();
p.Name="Test";
p.ImageStream=new byte[]{1,2,3};
String json=JsonConvert.SerializeObject(p);

在Javascript代碼中,您沒有將字節數組傳遞給C#方法,而是在傳遞字符串。 它們不是同一件事。 如果要傳遞字節數組,則它必須是一個實際的數字數組,其值在0到255之間。

像這樣嘗試:

var person = {
    "Name": "Test",
    "ImageStream": [65,81,73,68]
}

暫無
暫無

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

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