Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am consumed a web api in mvc controller.But no data has been returned.please help me to consume my api link.

What I have tried:

C#
public class HomenewController : Controller
    {
        // GET: Homenew
        string url = "https://api.yourmembership.com/reference/2_25/Sa_Members_Groups_Add.htm ";


        public async Task<ActionResult> Index()
        {
            List<Employee> EmpInfo = new List<Employee>();

            using (var client = new HttpClient())
            {
            
                client.BaseAddress = new Uri(url);

                client.DefaultRequestHeaders.Clear();
               
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

               
                HttpResponseMessage Res = await client.GetAsync(url);

          
                if (Res.IsSuccessStatusCode)
                {
             
                    var EmpResponse = Res.Content.ReadAsStringAsync().Result;

                  
                    EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);

                }
           
                return View(EmpInfo);
            }
        }
   
    }
}


Model for the api link
C#
public class Employee
  {
      public String ID { get; set; }
      public String GroupCode { get; set; }

      public Boolean AddAsMember { get; set; }
      public Boolean IsPrimaryGroup { get; set; }

      public Boolean AddAsGroupAdmin { get; set; }

      public String AddGroupRepTitle { get; set; }

  }
Posted
Updated 29-May-17 2:07am

1 solution

First you check your which properties are you passing from the api controller?

And You can try this type code in api controller:

/// <summary>
/// This method for Get All employee .
/// </summary>
[HttpGet]
[Route("GetEmployee")]
public HttpResponseMessage GetEmployee()
{
    try
    {
        ////get employee.
        var employeeCollection = employeeService.GetEmployee().ToList();
        ////check object
        if (employeeCollection.Count > 0 && employeeCollection != null)
        {
            ////dynamic list.
            dynamic employees = new List<ExpandoObject>();
            ////bind dynamic property.

            ////return response from employee service.
            foreach (var employeeDetail in employeeCollection)
            {
                ////bind dynamic property.
                dynamic employee = new ExpandoObject();

                ////map ids
                employee.ID = employeeDetail.ID;
                employee.GroupCode = employeeDetail.GroupCode;
                employee.AddAsMember = employeeDetail.AddAsMember;
                employee.IsPrimaryGroup = employeeDetail.IsPrimaryGroup;
                employee.AddAsGroupAdmin = employeeDetail.AddAsGroupAdmin;
                employee.AddGroupRepTitle = employeeDetail.AddGroupRepTitle;
                ////set customers values in list.
                employees.Add(employee);
                ////return customers service
                return this.Request.CreateResponse<List<ExpandoObject>>(HttpStatusCode.OK, (List<ExpandoObject>)employees);

            }
        }
        else
        {
            ////case of record not found.
            return this.Request.CreateResponse(HttpStatusCode.NotFound);
        }


    }
    catch (Exception ex)
    {
        //// handel exception log.
        Console.Write(ex.Message);

        ////return case of exception.
        return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Message = "Exception : " + ex.Message });
    }
}



hope it's useful for you...
 
Share this answer
 
v2
Comments
GrpSMK 29-May-17 8:23am    
thank you,this is YMSDK web api link. web api controller is need? for this
GrpSMK 29-May-17 8:28am    
https://api.yourmembership.com/reference/2_25/Sa_Members_Groups_Add.htm check this link and see the xml request and response ,i need response as my output in my mvc app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900