[英]Xamarin.Forms HTTP Post to Web Service - 404 Error
抱歉,是否已经有人问过这个问题,但是我似乎找不到与我的问题有关的问题。 我有一个使用C#Asp.Net Web API构建的Web服务,这里有以下POST方法:
[HttpPost]
[Route("AddLocation")]
public void PostLocation(Locations model)
{
using (Entities db = new Entities())
{
C_btblFALocation newLocation = new C_btblFALocation()
{
cLocationCode = model.Code,
cLocationDesc = model.Description
};
db.C_btblFALocation.Add(newLocation);
db.SaveChanges();
}
}
在我的Xamarin.Forms项目中,我有Rest Service类:
public async Task SaveLocationAsync(Locations item)
{
var uri = new Uri(string.Format(Constants.LocationSaveRestUrl));
try
{
var json = JsonConvert.SerializeObject(item);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@" Location successfully added.");
}
else
{
Debug.WriteLine(@" Oops, there seems to be a problem.");
}
}
catch (Exception ex)
{
Debug.WriteLine(@" ERROR {0}", ex.Message);
}
}
我的网址是在我的Constants类中设置的:
public static string LocationSaveRestUrl = "http://172.16.124.18/ArceusService/api/Assets/AddLocation/";
问题是我不断收到404错误。 我已经尽力尝试设置URL,但是没有运气。 数据似乎是通过调试传递的,但是我不知道POST方法的URL应该如何?
谢谢你的帮助!
您的client
变量如何声明? 通常,您设置一个BaseAddress
并且在Get/PostAsync
仅设置实际的资源URI。
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://something.com/api/");
var response = await client.GetAsync("resource/7");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.