2012-08-22 8 views
24

Ho un nuovo metodo in Web APILettura FromUri e FromBody allo stesso tempo

[HttpPost] 
public ApiResponse PushMessage([FromUri] string x, [FromUri] string y, [FromBody] Request Request) 

cui richiesta di classe è come

public class Request 
{ 
    public string Message { get; set; } 
    public bool TestingMode { get; set; } 
} 

sto facendo una query per localhost/Pusher/PushMessage ? x = foo & y = bar con PostBody:

{ Message: "foobar" , TestingMode:true } 

mi sto perdendo qualcosa?

risposta

26

Un corpo post è in genere una stringa URI come questo:

Message=foobar&TestingMode=true 

Bisogna fare in modo che l'intestazione HTTP contiene

Content-Type: application/x-www-form-urlencoded 

EDIT: Perché ancora non funziona, ho creato un esempio completo da solo.
Stampa i dati corretti.
Ho anche utilizzato .NET 4.5 RC.

// server-side 
public class ValuesController : ApiController { 
    [HttpPost] 
    public string PushMessage([FromUri] string x, [FromUri] string y, [FromBody] Person p) { 
     return p.ToString(); 
    } 
} 

public class Person { 
    public string Name { get; set; } 
    public int Age { get; set; } 

    public override string ToString() { 
     return this.Name + ": " + this.Age; 
    } 
} 

// client-side 
public class Program { 
    private static readonly string URL = "http://localhost:6299/api/values/PushMessage?x=asd&y=qwe"; 

    public static void Main(string[] args) { 
     NameValueCollection data = new NameValueCollection(); 
     data.Add("Name", "Johannes"); 
     data.Add("Age", "24"); 

     WebClient client = new WebClient(); 
     client.UploadValuesCompleted += UploadValuesCompleted; 
     client.Headers["Content-Type"] = "application/x-www-form-urlencoded"; 
     Task t = client.UploadValuesTaskAsync(new Uri(URL), "POST", data); 
     t.Wait(); 
    } 

    private static void UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) { 
     Console.WriteLine(Encoding.ASCII.GetString(e.Result)); 
    } 
} 
+1

E 'vero solo se uso la struttura MVC. Comunque questa è web api quindi l'associazione è diversa da mvc. Ma grazie per la tua risposta! – kkocabiyik

+0

Assicurarsi che l'intestazione HTTP contenga 'Content-Type: application/x-www-form-urlencoded'. –

+0

Non è possibile pubblicare come testo normale in web api mvc: S – kkocabiyik

0

È possibile utilizzare seguente codice per inviare JSON nel corpo della richiesta:

var httpClient = new HttpClient(); 
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

Request request = new Request(); 
HttpResponseMessage response = httpClient.PostAsJsonAsync("http://localhost/Pusher/PushMessage?x=foo&y=bar", request).Result; 

//check if (response.IsSuccessStatusCode) 
var createResult = response.Content.ReadAsAsync<YourResultObject>().Result; 
1

L'API Web utilizza regole di denominazione. Il metodo per un post deve essere avviato con Post.

È necessario rinominare il proprio PushMessage con il nome del metodo PostMessage.

Anche il web api defaulty ascolta (a seconda del percorso) a 'api/values ​​/ Message' e non a Pusher/Pushmessage.

[HttpPost] attributo non è richiesto

+0

Il metodo per un post dovrebbe essere avviato con Post. : Non richiesto –

+0

Questa risposta è sbagliata. Il metodo richiede "Post" solo nel nome quando non esiste un attributo [HttpPost]. – ehsan88