从服务器端而非客户端调用Api

时间:2019-08-07 09:27:59

标签: asp.net api

如何从服务器端调用请求获取(Api)。

这是服务器端

public string GetAllBook()
    {
        bookAssembly bookassembleur = new bookAssembly();
        bookList = bookassembleur.GetBooks();
    }

这是Api请求

 public List<Book> Get()
        {
            BookAssembly searchallbook = new BookAssembly();
            return searchallbook.GetBooks();


        bookAssembly bookassembleur = new bookAssembly();
        bookList = bookassembleur.GetBooks();
        DataTable dt = new DataTable();
        if (dt.Columns.Count == 0)
        {
            dt.Columns.Add("ID");
            dt.Columns.Add("Title");
            dt.Columns.Add("Price");
            dt.Columns.Add("Author");
            dt.Columns.Add("Qauntite");
            dt.Columns.Add("Categorie");
        }
        foreach (Book book in bookList)
        {
            DataRow NewRow = dt.NewRow();
            NewRow[0] = book.ID;
            NewRow[1] = book.Title;
            NewRow[2] = book.Price;
            NewRow[3] = book.Author;
            NewRow[4] = book.Qauntite;
            NewRow[5] = book.Categorie.Name;
            dt.Rows.Add(NewRow);
        }
        gvBook.DataSource = dt;
        gvBook.DataBind();
        return "";

    }

我要删除bookList = bookassembleur.GetBooks()并调用api

2 个答案:

答案 0 :(得分:0)

您必须使用Text('asdasdasda')访问它。

例如

HttpClient

P.S。这不是运行代码,只是一个想法。

答案 1 :(得分:0)

  public string GetAllBook()
         {
            DataTable dt = new DataTable();
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:6735/api/book");
                //HTTP GET
                var responseTask = client.GetAsync("Book");
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<Book[]>();
                    readTask.Wait();
                    var Books = readTask.Result;
                    if (dt.Columns.Count == 0)
                    {
                        dt.Columns.Add("ID");
                        dt.Columns.Add("Title");
                        dt.Columns.Add("Price");
                        dt.Columns.Add("Author");
                        dt.Columns.Add("Qauntite");
                        dt.Columns.Add("Categorie");
                    }
                    foreach (Book book in Books)
                    {
                        DataRow NewRow = dt.NewRow();
                        NewRow[0] = book.ID;
                        NewRow[1] = book.Title;
                        NewRow[2] = book.Price;
                        NewRow[3] = book.Author;
                        NewRow[4] = book.Qauntite;
                        NewRow[5] = book.Categorie.Name;
                        dt.Rows.Add(NewRow);
                    }
                }
            }
            gvBook.DataSource = dt;
            gvBook.DataBind();
            return "";

        }