Ejemplo n.º 1
0
    public string GetProductsJson(string prefix)
    {
        List <Product> products = new List <Product>();

        if (prefix.Trim().Equals(string.Empty, StringComparison.OrdinalIgnoreCase))
        {
            products = ProductFacade.GetAllProducts();
        }
        else
        {
            products = ProductFacade.GetProducts(prefix);
        }

        //your object is your actual object (may be collection) you want to serialize to json
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(products.GetType());
        //create a memory stream
        MemoryStream ms = new MemoryStream();

        //serialize the object to memory stream
        serializer.WriteObject(ms, products);
        //convert the serizlized object to string
        string jsonString = Encoding.Default.GetString(ms.ToArray());

        //close the memory stream
        ms.Close();
        return(jsonString);
    }
Ejemplo n.º 2
0
        public IActionResult getAllProd()
        {
            var products = Facade.GetAllProducts();

            return(Ok(products));
        }