Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Xamarin表单中的ASP Rest web Api?_C#_Rest_Xamarin.forms - Fatal编程技术网

C# 如何使用Xamarin表单中的ASP Rest web Api?

C# 如何使用Xamarin表单中的ASP Rest web Api?,c#,rest,xamarin.forms,C#,Rest,Xamarin.forms,我已经在ASP.NETWeb表单中创建了一个RESTAPI。我可以使用API从MySql数据库读写数据(我使用chrome的REST客户端扩展测试了API)。现在我正试图在我的跨平台xamarin表单中使用RESTAPI。我刚刚在我的应用程序中添加了一个按钮,如果我单击该按钮,它是否可以检索数据。我在var Json上插入了一个断点,以查看是否获得了任何数据。但我无法找回它。我正在本地主机中运行Web Api。我在VS Android emulator中运行该应用程序。请指导我如何正确使用RES

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

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/";
        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());
            }
        }
     }
}
namespace WorkApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类TestPage:ContentPage
{
私有字符串Uri=”http://192.168.0.124:59547/api/";
列出机器清单;
公共测试页()
{
初始化组件();
检查();
}
私有异步任务提交(对象发送方、事件参数)
{
var httpClient=新的httpClient();
httpClient.BaseAddress=Uri//I已经更改了Uri变量,您应该扩展这个类,并在构造函数中为它提供相同的基址。
var resp=wait httpClient.GetAsync(“机器”);
if(响应结果IsSuccessStatusCode)
{
var repStr=resp.Result.Content.ReadAsStringAsync();
machines=JsonConvert.DeserializeObject(repStr.Result.ToString());
}
}
}
}

关于这一点,有很多答案。请搜索一下。我已经搜索过了。我只需要一个关于如何正确使用我创建的API的简单示例。另外,有人回答了你的问题,但你没有接受任何问题。你认为人们会继续回答吗?我同意这里的@Yuri,如果你不回答/投票/接受作为答案,那些愿意帮助你的人就不会再帮助你了
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());
            }
        }
     }
}