Skip to main content
added 39 characters in body
Source Link
MarvinS
  • 84
  • 2
  • 11

So basically you have totwo options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Employees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the EmploeeysEmployees list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So if you combine the given code and my answer ist would be something like this:

    [HttpGet]
    [Route("AllEmployeeDetailsInJSON")]
    public IHttpActionResult GetEmployeeInJSON()
    {
        try
        {
            var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
            return Ok(employeesFromDb);
        }
        catch (Exception)
        {
            throw;
        }
    }

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Employees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So basically you have two options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Employees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Employees list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So if you combine the given code and my answer ist would be something like this:

    [HttpGet]
    [Route("AllEmployeeDetailsInJSON")]
    public IHttpActionResult GetEmployeeInJSON()
    {
        try
        {
            var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
            return Ok(employeesFromDb);
        }
        catch (Exception)
        {
            throw;
        }
    }
added 39 characters in body
Source Link
MarvinS
  • 84
  • 2
  • 11

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.CustomersEmployees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Customers.ToList();

        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Employees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;
added 1048 characters in body
Source Link
MarvinS
  • 84
  • 2
  • 11

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Customers.ToList();

        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(YourObjectemployessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

JsonConvert.SerializeObject(YourObject);

So basically you have to options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Customers.ToList();

        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Emploeeys list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;
Source Link
MarvinS
  • 84
  • 2
  • 11
Loading