2012-10-29 10 views
13

मैं एक अनुरोध कर रहा हूँ के साथ Json पोस्ट मान पाने के लिए WebAPI पोस्ट विधि एक asp.net करते हैं, और मैं एक अनुरोध चर प्राप्त करने में सक्षम नहीं beeing कर रहा हूँ।कैसे asp.net WebAPI

अनुरोध

jQuery.ajax({ url: sURL, type: 'POST', data: {var1:"mytext"}, async: false, dataType: 'json', contentType: 'application/x-www-form-urlencoded; charset=UTF-8' }) 
    .done(function (data) { 
     ... 
    }); 

वेब एपीआई Fnx

[AcceptVerbs("POST")] 
    [ActionName("myActionName")] 
    public void DoSomeStuff([FromBody]dynamic value) 
    { 
     //first way 
     var x = value.var1; 

     //Second way 
     var y = Request("var1"); 

    } 

मैं दोनों तरीकों में var1 सामग्री प्राप्त नहीं कर सकता है ... (जब तक मैं उस के लिए एक वर्ग बनाने)

मुझे यह कैसे करना चाहिए?

उत्तर

22

पहले तरीका आज़माएं:

public void Post([FromBody]dynamic value) 
    { 
     var x = value.var1.Value; // JToken 
    } 

ध्यान दें कि value.Property वास्तव में एक JToken उदाहरण देता है तो यह मूल्य value.Property.Value कॉल करने के लिए आप की जरूरत है पाने के लिए।

दूसरा तरीका:

public async Task Post() 
    {   
     dynamic obj = await Request.Content.ReadAsAsync<JObject>(); 
     var y = obj.var1; 
    } 

ऊपर काम फ़िडलर का उपयोग कर के दोनों। पहला विकल्प आप के लिए काम नहीं कर रहा है, तो यह सुनिश्चित करें कि JsonMediaTypeFormatter सामग्री deserialize करने के लिए प्रयोग किया जाता है application/json लिए सामग्री प्रकार सेट करके देखें।

+1

और यदि आप एक सहायक विधि में ऐसा करना चाहते हैं, और पूरी तरह से 'await' वाक्य रचना समझ में नहीं आता है, तो आप' Request.Content.ReadAsAsync () .Result [ "var1"] का उपयोग कर सकते ' (देखें [पूरा जवाब] (http://stackoverflow.com/a/19052895/1037948)) – drzaus

-5

नीचे दिए तरीक़े

[AcceptVerbs("POST")] 
[ActionName("myActionName")] 
public static void DoSomeStuff(var value) 
{ 
    //first way 
    var x = value; 
} 
6

इस पर थोड़ी देर के लिए चारों ओर मेरे सिर पीटने और कई अलग अलग बातें मैं API सर्वर पर कुछ breakpoints डाल समाप्त हो गया और महत्वपूर्ण मूल्य जोड़े के अनुरोध में नीचे भरवां पाया की कोशिश के बाद। मुझे पता था कि वे कहां थे, उन्हें एक्सेस करना आसान था। हालांकि, मुझे केवल WebClient.UploadString के साथ काम करने के लिए यह विधि मिली है। हालांकि, यह आसानी से पर्याप्त काम करते हैं और आप के रूप में कई मापदंडों को लोड करने के रूप में आप की तरह है और बहुत आसानी से उन्हें सर्वर साइड का उपयोग की अनुमति देता है है। ध्यान दें कि मैं लक्ष्य 4.5 को लक्षित कर रहा हूं।

ग्राहक के पक्ष

// Client request to POST the parameters and capture the response 
public string webClientPostQuery(string user, string pass, string controller) 
{ 
    string response = ""; 

    string parameters = "u=" + user + "&p=" + pass; // Add all parameters here. 
    // POST parameters could also easily be passed as a string through the method. 

    Uri uri = new Uri("http://localhost:50000/api/" + controller); 
    // This was written to work for many authorized controllers. 

    using (WebClient wc = new WebClient()) 
    { 
     try 
     { 
      wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 
      response = wc.UploadString(uri, login); 
     } 
     catch (WebException myexp) 
     { 
      // Do something with this exception. 
      // I wrote a specific error handler that runs on the response elsewhere so, 
      // I just swallow it, not best practice, but I didn't think of a better way 
     } 
    } 

    return response; 
} 

सर्वर साइड

// In the Controller method which handles the POST request, call this helper: 
string someKeyValue = getFormKeyValue("someKey"); 
// This value can now be used anywhere in the Controller. 
// Do note that it could be blank or whitespace. 

// This method just gets the first value that matches the key. 
// Most key's you are sending only have one value. This checks that assumption. 
// More logic could be added to deal with multiple values easily enough. 
public string getFormKeyValue(string key) 
{ 
    string[] values; 
    string value = ""; 
    try 
    { 
     values = HttpContext.Current.Request.Form.GetValues(key); 
     if (values.Length >= 1) 
      value = values[0]; 
    } 
    catch (Exception exp) { /* do something with this */ } 

    return value; 
} 

कैसे बहु मूल्य Request.Form कुंजी/मान जोड़े को संभालने के लिए पर अधिक जानकारी के लिए, देखें:

http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.110).aspx

0

इसे आजमाएं।

public string Post(FormDataCollection form) { 
    string par1 = form.Get("par1"); 

    // ... 
}