簡體   English   中英

Xamarin.Forms HTTP發布到Web服務-404錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM