2016-04-05 27 views
0

Json verilerini yayınladığım bir web api2 geliştiriyorum.Web api 2 - Async Post

bu api kod şudur: doğru db içine

public HttpResponseMessage Post(an_lavanderie an_lavanderie) 
{ 
    var response = new HttpResponseMessage(); 

    if (!ModelState.IsValid) 
    { 
     response = Request.CreateErrorResponse(HttpStatusCode.NotFound, new Exception("modello non valido")); 
    } 

    bool creato = _repoLavanderie.CreaLavanderia(an_lavanderie); 

    if (creato == true) 
    { 
     response = Request.CreateResponse(HttpStatusCode.OK); 
    } 
    else 
    { 
     response = Request.CreateErrorResponse(HttpStatusCode.NotFound, new Exception("Errore nella creazione")); 
    } 
    return response; 
} 

Bu kod yazma. DATA POST Benim kod şudur:

var risultato = PostDataAsync(); 

and the function is 

var lav1 = new Lavanderia() 
{ 
    rag_soc = "Ragione Sociale", 
    rag_soc2 = "Ragione sociale2", 
    indirizzo = "Via Crispi 27", 
    citta = "Ovada", 
    provincia = "AL", 
    isAttiva = "N", 
    portalkey = "sample string 1", 
    isPortalVisibile = "S", 
    cap = "15057" 

}; 

using (var client = new HttpClient()) 
{ 
    client.BaseAddress = new Uri("http://localhost:56040/"); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    var response = await client.PostAsJsonAsync("api/CreaLavanderia", lav1); 
    if (response.StatusCode == System.Net.HttpStatusCode.OK) 
    { 
     MessageBox.Show("Crezione effettuata correttamente"); 
    } 
    else 
    { 
     MessageBox.Show("Creazione non effettuata"); 
    } 
} 
return ""; 

Mesaj operasyonu Tamam, ama bekliyor ne zaman kovmayın. Webapi'den gelen İade Mesajı doğru mu? Sorun nerede?

Teşekkürler.

+0

Tarayıcınızın geliştirici araçlarında bir POST izlediğinizde hangi sonuçları alırsınız? – leanne

cevap

0

Siz beklediğinizi kullandığınızdan ve bunun ateşlemediğini söylüyorsanız, yönteminizin async anahtar sözcüğüyle işaretlendiğinden emin olun.

+0

Merhaba, PostDataAsync() genel async Görev PostDataAsync() 'dir. PostAsJsonAsync() web api'yi doğru şekilde çağırır. – dotnettortona

İlgili konular