Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I'm new to WebAPI, and this is my first time in creating one via visual studio with Entity Framework using c#. When i compile the program it doesn't give me an error. It let's me download the result instead of displaying it in json format in Internet Explorer.

I'm sorry if my english is bad or i can't explain my problem clearly. i just want to see the output in Postman or in Google Chrome.


<pre lang="c#"> using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebAPIDemo.Models;

namespace WebAPIDemo.Controllers
{
    public class EmployeeController : ApiController
    {
        private TestWebAPIEntities db = new TestWebAPIEntities();
        // GET api/CRUD  
        [ResponseType(typeof(IEnumerable<Employee>))]
        [Route("api/GetEmployees")]
        public IQueryable<Employee> GetEmployees()
        {
            return db.Employees;
        }

        //public IHttpActionResult GetEmployees()
        //{
        //    var companies = db.Employees.ToList();
        //    return Ok(new { results = companies });
        //}
        // GET api/CRUD/5  
        [ResponseType(typeof(Employee))]
        [Route("api/GetEmployee")]
        public IHttpActionResult GetEmployee(long id)
        {
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return NotFound();
            }
            return Ok(employee);
        }
        // PUT api/CRUD/5  
        [Route("api/PutEmployee")]
        public IHttpActionResult PutEmployee(long id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (id != employee.ID)
            {
                return BadRequest();
            }
            db.Entry(employee).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
        // POST api/CRUD  
        [Route("api/PostEmployee")]
        [ResponseType(typeof(Employee))]
        public IHttpActionResult PostEmployee(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            db.Employees.Add(employee);
            db.SaveChanges();
            return CreatedAtRoute("DefaultApi", new
            {
                id = employee.ID
            }, employee);
        }
        // DELETE api/CRUD/5  
        [Route("api/DeleteEmployee")]
        [ResponseType(typeof(Employee))]
        public IHttpActionResult DeleteEmployee(long id)
        {
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return NotFound();
            }
            db.Employees.Remove(employee);
            db.SaveChanges();
            return Ok(employee);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        [Route("api/EmployeeExists")]
        private bool EmployeeExists(long id)
        {
            return db.Employees.Count(e => e.ID == id) > 0;
        } 
 }
} 


What I have tried:

I tried changing the code from
C#
<pre>private TestWebAPIEntities db = new TestWebAPIEntities();
        // GET api/CRUD  
        [ResponseType(typeof(IEnumerable<Employee>))]
        [Route("api/GetEmployees")]
        public IQueryable<Employee> GetEmployees()
        {
            return db.Employees;
        }


to this:
C#
//public IHttpActionResult GetEmployees()
        //{
        //    var companies = db.Employees.ToList();
        //    return Ok(new { results = companies });
        //}
Posted
Updated 8-Oct-18 23:41pm
Comments
Kornfeld Eliyahu Peter 7-Oct-18 12:31pm    
Have you read it: https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results?

[HttpGet]
[ProducesResponseType(typeof(Employee), 200)]
[Route("api/GetEmployees")]
public IActionResult GetEmployees()
{

try
{
var data = db.Employees.ToList();
if (data == null)
return NotFound("Sorry, no records found");
return Ok(data);

}
catch (Exception exc)
{
return NotFound("Exception Error. Records Not Found");
}
}
 
Share this answer
 
public IEnumerable<Employee> GetEmployees()

{ 
   var companies = db.Employees.ToList();
     return Ok(companies);
}
 
Share this answer
 

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