Thursday, December 3, 2015

How to call ASP.NET WEB API from .Net Framework 2.0

In Vb.Net

 Public Function ReadAllData() As EmployeeEntityCollection
            Dim employee As EmployeeEntityCollection

   Using client As New WebClient()
        Dim _baseAddress As String =       System.Configuration.ConfigurationManager.AppSettings("ClientbaseAddress")                     
                client.Headers(HttpRequestHeader.Accept) = "application/json"
       client.BaseAddress = _baseAddress
       Dim ApiUrl As String = "Employee/ReadAllData/?EmployeeId=" + String.Empty + "&EmployeeName=" + String.Empty

employee= JsonConvert.DeserializeObject(Of  EmployeeEntityCollection)(client.DownloadString(ApiUrl))

           Return employee

   End Using

  End Function

In C#.Net


public EmployeeEntityCollection ReadAllData() 
{
       EmployeeEntityCollection employee;
   
   
      using (var client = new WebClient())
         {
 string _baseAddress=
System.Configuration.ConfigurationManager.AppSettings["ClientbaseAddress"];
    client.Headers[HttpRequestHeader.Accept] = "application/json";
client.BaseAddress = _baseAddress;
string ApiUrl = "Employee/ReadAllData/?EmployeeId=" + string.Empty + "&EmployeeName=" + string.Empty;

employee = JsonConvert.DeserializeObject<EmployeeEntityCollection>(client.DownloadString(ApiUrl));

    return employee

         }

}




No comments:

Post a Comment