1
 public IEnumerable<Product> GetAllProdcut()
 {
     var products= _productRepository.GetAllProduct();
     return products;
 }

The above code returns the following JSON object:

[  
    {  
      "$id":"1",
      "Id":1,      
      "Name":"Dave",
      "Department":"IT",
    },
    {  
       "$id":"2",
      "Id":2,      
      "Name":"Dave",
      "Department":"IT",
    },
    {  
      "$id":"3",
      "Id":3,      
      "Name":"Dave",
      "Department":"IT",
    },
    {  
       "$id":"4",
      "Id":4,      
      "Name":"Dave",
      "Department":"IT",
    }
]

What I need is to add { "data": at the beginning and } at the end.

What's the best way to add { "data": at the beginning and } at the end?

I don't want to use string concatenation.

Thank you.

1 Answer 1

5

You just have to return an anonymous object:

public object GetAllProduct()
{
    var products= _productRepository.GetAllProduct();
    return new { data = products };
}

Here is a fiddle demo - https://dotnetfiddle.net/nKRsnQ

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.