L A

L A

  • 2k
  • 170
  • 152.9k

Can't return a type of IHttpActionResult - WebAPI

Jun 28 2017 1:38 AM
Hello all,
 
Firstly, this question is about WebAPI. But as i didnt found WebAPI under Category Dropdown, i posted under .NET General.
 
I'm working on MVC WebAPI, where WebAPI Performs a GET operation. But i'm unable to return type of IHttpActionResult.
Below is my WebAPI GET Action method.
  1. public IHttpActionResult GetDataFromDB()  
  2. {  
  3.     try  
  4.     {  
  5.         using (var ctx = new AdventureWorks2012Entities())  
  6.         {  
  7.             var query = (from d in ctx.Departments  
  8.                          join h in ctx.EmployeeDepartmentHistories
  9.  on d.DepartmentID equals h.DepartmentID  
  10.                          join s in ctx.Shifts on h.ShiftID equals s.ShiftID  
  11.                          join e in ctx.Employees
  12. on h.BusinessEntityID equals e.BusinessEntityID  
  13.                          join p in ctx.People
  14. on h.BusinessEntityID equals p.BusinessEntityID  
  15.                          select new  
  16.                          {  
  17. Name = ((p.FirstName == null ? "" : p.FirstName) + " " +
  18.  (p.MiddleName == null ? "" : p.MiddleName) + " " +
  19. (p.LastName == null ? "" : p.LastName)),  MaritalStatus = (e.MaritalStatus == "M" ? "Married" : "Single"),  
  20. Gender = (e.Gender == "M" ? "Male" : "FeMale"),  
  21. Designation = e.JobTitle,  
  22. Department = d.Name,  
  23. MajorBranch = d.GroupName  
  24.                          }).ToList();  
  25.             if (query.Count == 0)  
  26.             {  
  27.                 return NotFound();  
  28.             }  
  29.             return Ok(query);  
  30.         }  
  31.     }  
  32.     catch (Exception) { return null; }  
  33.     finally  
  34.     {  
  35.   
  36.     }  

Above code is working fine when i debug & Line # 29 show data in List format. But on browser it displaying as Error.
 
 
I can view output on Fiddler.
 
Please suggest me to figure out my mistake & achieve this.
 
Thanks in advance.

Answers (3)