如何在URL路径中使用Id。

时间:2017-03-07 12:49:41

标签: c# .net url xamarin

目标:从表中获取正确的ID并在我的网址路径中使用此ID。

我目前正在申请。在应用程序中,应该显示一个列表,用户可以在其中选择一个元素。选择此元素会将用户发送到新页面,在该页面中可以更新某些信息。我的问题在于更新过程。我无法获得正确的ID并在我的PUT请求的URL路径中使用它。

来自dataService.cs文件的我的PUT请求:

public async Task UpdateData(Post item)
        {
            const string Url = "http://localhost:5000/checklist/{0}";
            var uri = new Uri(string.Format(Url, item.Id));
            var data = JsonConvert.SerializeObject(item);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var response = await _client.PutAsync(uri, content); 
        }

PUT请求是从Checklist.cs文件中的此函数触发的:

async void Button_Clicked(object sender, System.EventArgs e)
    {
        Post newItem = new Post
        {
            DeviationDescription = DeviationDescript.ToString(),
            Date = DateTime.Now
        };
        dataService ds = new dataService();
        await ds.UpdateData(newItem);
        //RefreshData();
    }

1 个答案:

答案 0 :(得分:0)

您未在newPost中设置ID。

您必须从客户端进行设置,每次都可以使用Guid.NewGuid().ToString()获取新ID。