2017-05-17 44 views
0

當我嘗試使用Ajax傳遞值是這樣的:Ajax來.NET無法綁定多個參數誤差

$.ajax({ 
    type: "PUT", 
    dataType: 'json', 
    url: "http://localhost:52930/api/person/", 
    contentType: "application/json; charset=utf-8", 
    data: {persoon1, persoon2, file} 
}) 
    .done(function(msg) { 
     alert("Test") 
    }); 

在其中PERSON1和2是一個對象,和一個文件是一個字節數組(JSON )。

在.NET

public IHttpActionResult Put(Person personFrom , Person personTo , byte[] file) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(); 
     } 
     _personRepository.EncrypFileAndSendToPerson(file, personFrom , personTo); 
     return CreatedAtRoute("DefaultApi", new { controller = "Person", id = personTo.Id }, personTo); 
    } 

使用此功能,這是人類:

public class Person 
{ 
    private RSAWithRSAParameterKey _rsaParameters; 
    public Person() 
    { 
     _rsaParameters = new RSAWithRSAParameterKey(); 
     _rsaParameters.AssignNewKey(); 
     PrivateKey = _rsaParameters._privateKey; 
     PublicKey = _rsaParameters._publicKey; 
    } 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Password { get; set; } 
    public string Salt { get; set; } 
    public byte[] File { get; set; } 
    public string PrivateKey { get; set; } 
    public string PublicKey { get; set; } 
    public EncryptedPacket Packet { get; set; }  
} 

爲什麼我不斷收到這個錯誤,我該如何解決?

完整的錯誤

{Message: "An error has occurred.",…} 
ExceptionMessage 
: 
"Can't bind multiple parameters ('personFrom' and 'file') to the request's content." 
ExceptionType 
: 
"System.InvalidOperationException" 
Message 
: 
"An error has occurred." 
StackTrace 
: 
" at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) 
↵ at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() 
↵--- End of stack trace from previous location where exception was thrown --- 
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
↵ at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext() 
↵--- End of stack trace from previous location where exception was thrown --- 
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()" 

編輯:

這是當我CONSOLE.LOG)在data

{"personFrom":{"Id":2,"Name":"Sinan","Password":"wachtwoord2","Salt":"22ad8a4c-3175-4049-a9e0-f4f383c1cd09","File":null,"PrivateKey":"<RSAKeyValue><Modulus>3A86Zo99HaROAME/yU7POcogo4Zru62F+7PAXONv3Jm8l2RMfb4QlcCF483kXV85paVpbgI7en3O5eiFoTEUVUXP9InjpNg7X9jah8tJFLRvzIU74vHP7ccLXgOV5ztGUEGW2ESbpT6gkvNS6XvMyMS+r8rQt1mvNp3lhAErK81a6Pr1npVmew9cghikY5NOk5WcaAeh8pB4hPeGbTg33BRzEN+w8IeJh73EwKDz8fKklqbFv90qRUPciKKUStyCxTCU4yg43de4qcuzNXMXqjFF8NY0JXu+6L6yuRvrdFoBN4OWe2GLEGxVoemDWvO7rwP9qkSq51Bk9CnW4bd98w==</Modulus><Exponent>AQAB</Exponent><P>9dfUtHHiLbySstMMquL0QTazj8AI/S2IBpJX9kXgRQ9SaJ3l6VlbMmSTvjY3MgRK6OF3MISMlBRqAa0ptxttXyh1fEvS7B44XVrOOREEQQkRBo5Ly02vXFYudBApgd68EijBDcI0l3v7CzQu6Ng/ROLAYnrd4K9AzZ+cppRZ6tE=</P><Q>5SayKSK/R6I944RMeA/DYfep/KXiePSyE/MyI2A72q2bG8MaLG8w1JN1p5T5tfB2Tgyn/ePHjVoP7CYoRQz6PIcyxDDqRzyw5tDpQ0Rc3lTcJkM1WmWyB+9NDIV1mmhDgyUWAssS0IhKg/6HOat2dBbEPE+d9qgi9tjfQetKRY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rQt1mvNp3lhAErK81a6Pr1npVmew9cghikY5NOk5WcaAeh8pB4hPeGbTg33BRzEN+w8IeJh73EwKDz8fKklqbFv90qRUPciKKUStyCxTCU4yg43de4qcuzNXMXqjFF8NY0JXu+6L6yuRvrdFoBN4OWe2GLEGxVoemDWvO7rwP9qkSq51Bk9CnW4bd98w==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>","Packet":null},"personTo":{"Id":3,"Name":"Lennert","Password":"wachtwoord3","Salt":"daa4ccff-5764-41eb-b151-8b3cbc0f870b","File":null,"PrivateKey":"<RSAKeyValue><Modulus>44KWMkW7KFrnaK4qD90VSfiwLdttfpOYK+pApqXSAg2NnFLGxvI7AHoOWwQEQiYtQwyDjkNuxWsWxhYeMV9XwNaoba6hH/4iFx4ZR68FCJD5KsSfPuk+X4cf/R5auMKvLboKOFz9yRFYnBN17uQyI8NPhjTaAIRPJ5eTr/H+D6ug9hnwtz7tc/vpBPyjyomTcnUa9GVOrYAQ0RtsAvTJ2of9mT3G67WUhzIMqKsAFP6mObyAcyFgQx0uFdVFSFCOw26a0ASZM3juEVDT4OSWIZz3tWLD2acSPJTNxJFyvUMeEyc4dLPnPgZF/VypIjnuu4CZ/pplqS6N5Z3QhWgS2Q==</Modulus><Exponent>AQAB</Exponent><P>+Kk3iG6f03CG8yPWvN0CfkpHpVq2yvSguujscE6ZGW57cPYP3J02quqGvnCT5UUNxiyZt1tu/xh4HcapT2HVN1H4kCZdsKdbovGvjdAHzgZdNXunpLzIwvCbp9wskOqGClyZXKO1kEFhQ+u+69PuH9ifE8yxvzanfLc5irNY908=</P><Q>6jmP4ikBIawVTk5DVKKbvw9tkN0GdbTzMU7wTYHYoh61/k438AoOcnngr96orbqSQNtFucQ5IRonkHDqjjZrPlCYh+VpGCKR4Vl3+TWBc/ad1xiBRcmgfGyd72CRqYU3sjsfNe1zYkgvJqpUf0frafUz2tqBR+Bk/jC/RAhRyVc=</Q><DP>Ch6AxwmRcX5Tz5M2yurHjjIhpkYBy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yjyomTcnUa9GVOrYAQ0RtsAvTJ2of9mT3G67WUhzIMqKsAFP6mObyAcyFgQx0uFdVFSFCOw26a0ASZM3juEVDT4OSWIZz3tWLD2acSPJTNxJFyvUMeEyc4dLPnPgZF/VypIjnuu4CZ/pplqS6N5Z3QhWgS2Q==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>","Packet":null},"file":"{\"1\":115,\"2\":100,\"3\":100,\"4\":115,\"5\":100,\"6\":97}"} 

回答

1

最有可能出現這種情況,因爲參數是不可選的,您不會將FromBody屬性應用於參數。試試這個:

public IHttpActionResult Put(
    [FromBody] Person personFrom, 
    [FromBody] Person personTo, 
    [FromBody] byte[] file) 
{ 
    ... 
} 

如果沒有幫助嘗試創建新模型映射請求,將包含所有屬性:

public class PutModel 
{ 
    public Person PersonFrom {get; set;} 
    public Person PersonTo {get; set;} 
    public byte[] File {get; set;} 
} 

而在你的動作定義一個參數:

public IHttpActionResult Put([FromBody] PutModel model) 
{ 
    ... 
} 

並確保屬性名稱正確@Jay建議。

data: { 
    "personFrom": persoon1, 
    "personTo": persoon2, 
    "file": file 
} 

此外,基於控制檯中的消息,文件是一個對象,而不是JS中的數組。儘量明確地使一個數組像Converting a JS object to an array

var array = $.map(file, function(value, index) { 
    return value; 
}); 
+0

它現在提供400個不好的請求。 –

+0

[[123],[34],[49],[34],[58],[49],[49],[53],[44],[34],[50],[34], [58],[49],[48],[48],[44],...]這就是數組現在看起來如此 –

+0

@SinanSamet,數組看起來不對,應該沒有一個值的內部數組。只是簡單的數組。 400是因爲你'返回BadRequest();'我想。 –

0

我收到(該值的數據需要傳遞的價值像這種方式

$.ajax({ 
    type: "PUT", 
    dataType: 'json', 
    url: "http://localhost:52930/api/person/", 
    contentType: "application/json; charset=utf-8", 
    data: { 
     "personFrom": persoon1, 
     "personTo": persoon2, 
     "file": file 
    } 
}) 
     .done(function (msg) { 
      alert("Test") 
     }); 
+0

仍然給了同樣的錯誤 –

+0

是您的數據是有效的格式化或不? – Jay

+0

我編輯我的問題,包含我發送的確切數據 –

0

試試下面的代碼

$.ajax({ 
        type: 'PUT', 
        url: 'http://localhost:52930/api/person/', 
        dataType: 'application/json', 
        data: { 
         personFrom : persoon1, 
         personTo : persoon2, 
         file: file      
        }, 
        success: function (data) { 
         alert("Application Updated Successfully"); 
        }, 
        error: function (err) { 
        } 
       }); 

你想填充在JavaScript狀物體:

var persoon1 = { 
Id: $("#id").val(), 
Name: $("#Name").val(), 
} 

你的方法是這樣的:

相關問題