首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web api 2-异步发布

Web api 2-异步发布
EN

Stack Overflow用户
提问于 2016-04-05 22:02:52
回答 1查看 141关注 0票数 0

我正在开发一个web api2,我在那里发布json数据。

该接口的代码为:

代码语言:javascript
复制
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;
}

这段代码可以正确地写入数据库。我发布数据的代码如下:

代码语言:javascript
复制
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 "";

Post操作是可以的,但是当等待时不要触发。有没有可能webapi返回的消息不正确?问题出在哪里?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-04-05 22:10:10

由于您使用的是await,并且您说它没有触发,因此请确保您的方法使用关键字async进行标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36428630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档