如何以 Xamarin 形式使用 ASP Rest web Api? [英] How to consume ASP Rest web Api in Xamarin forms?

查看:16
本文介绍了如何以 Xamarin 形式使用 ASP Rest web Api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.Net Web 表单中创建了一个 REST Api.我可以使用 API 从 MySql 数据库读取和写入数据(我使用 chrome 的 REST 客户端扩展测试了 Api).现在我试图在我的跨平台 xamarin 表单中使用 Rest Api.我刚刚在我的应用程序中添加了一个按钮,如果我单击该按钮,看看它是否可以检索数据.我在 var Json 处插入了一个断点,以查看是否获得了任何数据.但我无法检索它.我在本地主机中运行 Web Api.我在 VS Android 模拟器中运行该应用程序.请指导我如何正确使用 REST Web 服务.谢谢.

I have created a REST Api in ASP.Net web form. I can read and write data from MySql database using the API (I tested the Api using chrome's REST Client extension). Now I am trying to consume the Rest Api in my cross platform xamarin forms. I just added a button in my app and see whether it can retrieve data if I click the button. I inserted a breakpoint at the var Json to see whether I'm getting any data. But I am not able to retrieve it. I am running the Web Api in localhost. I run the app in VS Android emulator. Please guide me on how to properly consume the REST web service. Thank you.

Web 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);

    }



}
}

推荐答案

这就是我的答案:

namespace WorkApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestPage : ContentPage
    {
        private string Uri = "http://192.168.0.124:59547/api/";
        List<Machine> machines;
        public TestPage ()
        {
          InitializeComponent ();
          check();
        }


        private async Task submit(object sender, EventArgs e)
        {
           var httpClient = new HttpClient();
           httpClient.BaseAddress = Uri // I have changed the Uri variabele, you should extend this class and give it the same base address in the constructor.
           var resp= await httpClient.GetAsync("Machines");
            if (resp.Result.IsSuccessStatusCode)
            {
                var repStr = resp.Result.Content.ReadAsStringAsync();
                machines= JsonConvert.DeserializeObject<List<Machine>>(repStr.Result.ToString());
            }
        }
     }
}

这篇关于如何以 Xamarin 形式使用 ASP Rest web Api?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆