2017-06-16 6 views
5

जब पैरामीटर प्रदान करने के मैं त्रुटि मिलती है बाध्य नहीं किया जा सकता,MVC वेब एपीआई, त्रुटि: एक से अधिक पैरामीटर

"Can't bind multiple parameters"

यहाँ मेरी कोड है

[HttpPost] 
public IHttpActionResult GenerateToken([FromBody]string userName, [FromBody]string password) 
{ 
    //... 
} 

अजाक्स:

$.ajax({ 
    cache: false, 
    url: 'http://localhost:14980/api/token/GenerateToken', 
    type: 'POST', 
    contentType: "application/json; charset=utf-8", 
    data: { userName: "userName",password:"password" }, 

    success: function (response) { 
    }, 

    error: function (jqXhr, textStatus, errorThrown) { 

     console.log(jqXhr.responseText); 
     alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText + " " + jqXhr.status); 
    }, 
    complete: function (jqXhr) { 

    }, 
}) 
+0

संभावित डुप्लिकेट (पेलोड भेजने के लिए सुनिश्चित करने के लिए कार्रवाई को अद्यतन https://stackoverflow.com/questions/14407458/webapi- बहु-put-post-parameters) –

+0

प्रिय पॉल। मैंने अभी उल्लेख किया गया प्रश्न चेक किया है यह डुप्लिकेट नहीं है क्योंकि यह प्रश्न मेरे वर्तमान प्रश्न से अलग है। धन्यवाद – Tom

+0

क्या आप वेब एपीआई 1 या 2 का उपयोग कर रहे हैं? –

उत्तर

11

संदर्भ: Parameter Binding in ASP.NET Web API - Using [FromBody]

At most one parameter is allowed to read from the message body. So this will not work:

// Caution: Will not work!  
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... } 

The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.

जोर मेरा

कहा जा रहा है। अपेक्षित समेकित डेटा को स्टोर करने के लिए आपको एक मॉडल बनाने की आवश्यकता है।

public class AuthModel { 
    public string userName { get; set; } 
    public string password { get; set; } 
} 

और फिर शरीर में है कि मॉडल की उम्मीद

[HttpPost] 
public IHttpActionResult GenerateToken([FromBody] AuthModel model) { 
    string userName = model.userName; 
    string password = model.password; 
    //... 
} 

ठीक से

var model = { userName: "userName", password: "password" }; 
$.ajax({ 
    cache: false, 
    url: 'http://localhost:14980/api/token/GenerateToken', 
    type: 'POST', 
    contentType: "application/json; charset=utf-8", 
    data: JSON.stringify(model), 
    success: function (response) { 
    }, 

    error: function (jqXhr, textStatus, errorThrown) { 

     console.log(jqXhr.responseText); 
     alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText + " " + jqXhr.status); 
    }, 
    complete: function (jqXhr) { 

    }, 
}) 
[वेबएपीआई एकाधिक रखें/पोस्ट मापदंडों] की
संबंधित मुद्दे