locked
Consume Api using RestSharp RRS feed

  • Question

  • User1979860870 posted

    Hi

      I have below code & i want to display in Jquery Datatable. 

    if (!this.IsPostBack)
            {
                RestClient restClient = new RestClient("http://localhost:40991/api/Employees");
                restClient.Timeout = -1;
                RestRequest request = new RestRequest(Method.GET);
    
                request.AddHeader("Content-Type", "application/json");
                IRestResponse result = restClient.Execute(request);
    
                var response = restClient.Execute(request);
    
                if (response.Content == null)
                    throw new Exception(response.ErrorMessage);
    
            }

    Thanks

    Tuesday, February 23, 2021 6:15 AM

All replies

  • User303363814 posted

    What can you do and, more importantly, what can't you do?

    Do you now anything about Jquery?  Have you used it before?

    Which datatable do you want to use?  Have you used it before?

    What work have you done on your requirement?

    Tuesday, February 23, 2021 9:13 PM
  • User-1330468790 posted

    Hi jagjit saini,

     

    Before providing you with the solution, I recommend that you can directly use server processing mode of the jQuery Datatables with the web api url.

       

    Within the current codes, I think you could fetch the values from the api and bind the data to the grid view control.

    Then, in the page load function, you could use the client id of the grid view control and make it decorated with DataTables.

     

    More details, you could refer to below codes:

    model (Employee):

    public class Employee
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
    

    Api returned values:

    new List<Employee>
    {
                    new Employee{Id=1,Name="Name1"},
                    new Employee{Id=2,Name="Name2"},
                    new Employee{Id=3,Name="Name3"}
    }

    aspx:

     <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </form>
        <script>
            $(function () {
                $("#<%=GridView1.ClientID%>").DataTable();
            })
        </script>

    Code behind:

    if (!this.IsPostBack)
                {
    // change the port number as yours RestClient restClient = new RestClient("https://localhost:40991/api/GetEmployees"); restClient.Timeout = -1; RestRequest request = new RestRequest(Method.GET); request.AddHeader("Content-Type", "application/json"); var response = restClient.Execute<List<Employee>>(request); if (response.Content == null) throw new Exception(response.ErrorMessage); GridView1.DataSource = response.Data; GridView1.DataBind(); GridView1.HeaderRow.TableSection = TableRowSection.TableHeader; }

    Demo:

     

    Hope helps.

    Best regards,

    Sean



     

     

    Wednesday, February 24, 2021 10:28 AM