2012-08-22 13 views
24

mam nową metodę w Web APIReading FromUri i FromBody jednocześnie

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

gdzie prośba klasa jest jak

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

Robię zapytanie do localhost/Pusher/PushMessage ? x = y = foo & bar z PostBody:

{ Message: "foobar" , TestingMode:true } 

Am I czegoś brakuje?

Odpowiedz

26

Słupek ciało jest zazwyczaj ciągiem URI tak:

Message=foobar&TestingMode=true 

Musisz upewnić się, że nagłówek HTTP zawiera

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

EDIT: Bo to nadal nie działa, ja sam stworzyłem pełny przykład.
Umożliwia wydrukowanie prawidłowych danych.
Użyłem również .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

Prawdą jest tylko wtedy, gdy używam mvc strukturę. Jednak jest to api web, więc powiązanie różni się od mvc. Ale dzięki za odpowiedź! – kkocabiyik

+0

Upewnij się, że nagłówek HTTP zawiera "Content-Type: application/x-www-form-urlencoded". –

+0

Nie możesz wysłać zwykłego tekstu do web api mvc: S – kkocabiyik

0

Możesz użyć poniższy kod, żeby pisać json w żądaniu ciała:

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

API Web wykorzystuje regulacje nazewnictwa. Metoda postu powinna zaczynać się od Post.

Należy zmienić nazwę PushMessage na nazwę metody PostMessage.

Również web api wadliwy słucha (w zależności od trasy) do "api/wartości/wiadomość", a nie do Pusher/Pushmessage.

[HttpPost] atrybut nie jest wymagane

+0

Metoda dla postu powinna zaczynać się od Post. : Nie wymagane –

+0

Ta odpowiedź jest błędna. Metoda wymaga nazwy "Post" tylko wtedy, gdy nie ma atrybutu [HttpPost]. – ehsan88