I have created a web api to retrieve values from the database. The data retrieved are in the form of xml but I want it as json data.
CustomerDetailsController.cs :
using WebApiDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApiDemo.Controllers
{
public class CustomerDetailsController : ApiController
{
public static IList<CustomerDetail> listCusDt = new List<CustomerDetail>()
{
};
[AcceptVerbs("GET")]
public CustomerDetail RPCStyleMethodFetchFirstCustomerDetails()
{
return listCusDt.FirstOrDefault();
}
[HttpGet]
[ActionName("GetAllCustomerDetails")]
public List<CustomerDetail> Get(string id)
{
SqlDataReader reader = null;
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.StoredProcedure;
//sqlCmd.Parameters.Clear();
sqlCmd.CommandText = "getBasketsForCustomer";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@CustomerId", id);
myConnection.Open();
reader = sqlCmd.ExecuteReader();
List<CustomerDetail> customerDetails = new List<CustomerDetail>();
while (reader.Read())
{
CustomerDetail cusdt = new CustomerDetail();
cusdt.OrderId = Convert.ToInt32(reader.GetValue(0));
cusdt.CustomerId = reader.GetValue(1).ToString();
cusdt.OrderDate = reader.GetValue(2).ToString();
cusdt.ProductName = reader.GetValue(3).ToString();
cusdt.Quantity = Convert.ToInt32(reader.GetValue(4));
cusdt.UnitPrice = Convert.ToInt32(reader.GetValue(5));
cusdt.Amount = Convert.ToInt32(reader.GetValue(6));
customerDetails.Add(cusdt);
}
return customerDetails;
myConnection.Close();
}
}
}
CustomerDetail.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApiDemo.Models
{
public class Customer
{
public string CustomerId { get; set; }
public string CompanyName { get; set; }
public string City { get; set; }
}
}