Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ASP.net Web API and this is my get method:

C#
LyricContext db = new LyricContext();
public IEnumerable<MusicDTO> Get()
{
    var json = from M in db.Musics
               select new MusicDTO
               {
                   ID = M.ID,
                   MusicName = M.MusicName,
                   Lyric = M.Lyric,
                   Albume = M.Albume.AlbumeName,
                   Artist = M.Albume.Artist.ArtistName
               };
    return json;


this return me something like this:

C#
[
{
    "ID": "9245cd1e-c161-4c0b-ab43-25cef43b48b1",
    "Lyric": "I'm on a boat",
    "MusicName": "on a boat",
    "Albume": "New Albume",
    "Artist": "example"
},
...
]

but I need that my array has a name like this:

C#
{
"musics":    [
{
    "ID": "9245cd1e-c161-4c0b-ab43-25cef43b48b1",
    "Lyric": "I'm on a boat",
    "MusicName": "on a boat",
    "Albume": "New Albume",
    "Artist": "example"
},
...
]



what should I do?
Posted
Updated 25-Mar-19 1:16am

 
Share this answer
 
LyricContext db = new LyricContext();
public HttpResponseMessage  Get()
{
    var json = from M in db.Musics
               select new MusicDTO
               {
                   ID = M.ID,
                   MusicName = M.MusicName,
                   Lyric = M.Lyric,
                   Albume = M.Albume.AlbumeName,
                   Artist = M.Albume.Artist.ArtistName
               };
    //return json;

    if (json.Any())
    {
        return Request.CreateResponse(HttpStatusCode.OK, new { musics = json.OrderBy(c => c.ID) });
     }
    else 
    {
        return Request.CreateResponse(HttpStatusCode.NotFound, new { musics = "No Data Available" });

    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900