我想從Windows Phone應用程序發佈一個json對象到web api項目,但我仍然得到404錯誤。對於POST方法,我使用的代碼:使用httpClient窗口的郵件JSON對象Phone Phone 8.1
Mail mailToCheck = new Mail();
try
{
mailToCheck.MailProfil = TxtBox_mail.Text.ToString();
string json = JsonConvert.SerializeObject(mailToCheck);
var httpClient = new System.Net.Http.HttpClient(new HttpClientHandler());
System.Net.Http.HttpResponseMessage response = await httpClient.PostAsync(new Uri("http://uri/api/Profil/CheckMail"), new StringContent(json));
var responseString = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.HResult.ToString());
}
我conctroller的方法CheckMail:
[HttpPost]
[Route("api/Profil/CheckMail")]
public IHttpActionResult CheckMail([FromBody]Mail MailProfil)
{
if (MailProfil.MailProfil != null)
{
try
{
bool exists = Librairie.Profils.mailExists(MailProfil.MailProfil);
return Ok(exists);
}
catch(Exception ex)
{
return InternalServerError(ex);
}
}
else
{
return BadRequest();
}
}
的郵件對象恰恰是在應用程序一樣的web api項目。有人可以告訴我我在做什麼錯嗎?
嘗試..'等待httpClient.PostAsJsonAsync(新的URI( 「HTTP:// URI/API/PROFIL/CheckMail /」),mailToCheck );'並添加最後一個'/' –
Milen
2014-11-14 12:49:38
是從HttpClient的方法或我需要添加一些使用?我昨天試過,但找不到方法,只需PostAsync – 2014-11-14 14:11:43
您需要添加對System.Net.Http.Formatting.dll的引用:) – Milen 2014-11-14 15:05:46