-2
我在ASP.Net web窗體中創建了一個REST Api。我可以使用API從MySql數據庫讀取和寫入數據(我使用chrome的REST客戶端擴展測試了Api)。現在我試圖在我的跨平臺xamarin表單中使用Rest Api。我只是在我的應用程序中添加了一個按鈕,看看它是否可以檢索數據,如果我點擊按鈕。我在var Json中插入了一個斷點,以查看我是否獲取了任何數據。但我無法檢索它。我在本地主機上運行Web Api。我在VS Android模擬器中運行應用程序。請指導我如何正確使用REST Web服務。謝謝。如何使用ASP在Xamarin表單中留下web Api?
網絡API
namespace WorkAppApi.Controllers
{
public class MachinesController : ApiController
{
// GET: api/Machines
public List<machines> Get()
{
DBConn db = new DBConn();
return db.getMachineList();
}
// GET: api/Machines/5
public machines Get(long id)
{
DBConn db = new DBConn();
machines m = db.getMachine(id);
return m;
}
// POST: api/Machines
public HttpResponseMessage Post([FromBody]machines value)
{
DBConn db = new DBConn();
long id;
id = db.addMachine(value);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
response.Headers.Location = new Uri(Request.RequestUri, String.Format("machines/{0}", id));
return response;
}
// PUT: api/Machines/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Machines/5
public void Delete(int id)
{
}
}
}
功能提交按鈕。
namespace WorkApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestPage : ContentPage
{
private string Uri = "http://192.168.0.124:59547/api/Machines/";
public TestPage()
{
InitializeComponent();
check();
}
private async Task submit(object sender, EventArgs e)
{
var httpClient = new HttpClient();
var json = await httpClient.GetAsync(Uri);
}
}
}
有在SO噸答案這個問題。請做一些搜索。 –
我已經完成了搜索。我只需要一個關於如何正確使用我創建的API的簡單示例。 – jdk
此外,人們回答你的問題,但你沒有接受任何問題。你認爲人們會繼續回答嗎? –