Estoy usando asp.net core. Necesito mostrar los datos en el menú desplegable de selección en reaccionar desde la API web. Método GET de WebApi que devuelve SQL DataReader . Reader devuelve una fila con prodid , prodname y proddescr . Por favor, ayude cuál es la mejor manera de escribir una API web que use SQL DataReader para completar el menú desplegable de selección en reaccionar.
[HttpGet("{ProductID}")] public JsonResult GetProductInfo(int ProductID) { var response = GetProductInfo(ProductID); return new JsonResult(response); } public string GetProductInfo(int Product_ID) { SqlConnection objConnect = new SqlConnection(_connectionString); SqlCommand objCommand = new SqlCommand("usp_GetProdInfo", objConnect); objCommand.CommandType = CommandType.StoredProcedure; objCommand.Parameters.Add(new SqlParameter("@Product_ID", SqlDbType.Int, 4)); objCommand.Parameters["@Product_ID"].Value = intProduct_ID; string json = string.Empty; List<object> objects = new List<object>(); objConnect.Open(); SqlDataReader reader = objCommand.ExecuteReader(); while (reader.Read()) { IDictionary<string, object> record = new Dictionary<string, object>(); for (int i = 0; i < reader.FieldCount; i++) { record.Add(reader.GetName(i), reader[i]); } objects.Add(record); } json =JsonConvert.SerializeObject(objects); reader.Close(); objConnect.Close(); return json; }no necesita serializar datos, Net puede hacerlo por usted. Y es un estilo de programación muy malo usar objetos en lugar de clases reales. Así que devuelve List en lugar de List
public ActionResult<List<Product>> GetProductInfo(int ProductID) { var response = GetProductInfoList(ProductID); if (response != null) return Ok(result) else retun BadRequest(); } public List<Product> GetProductInfoList(int Product_ID) { List<Product> products = new List<Product>(); ...fix the code to get typed List<Product> from reader return products; }