question

KALYANAALLAM-4847 avatar image
0 Votes"
KALYANAALLAM-4847 asked AgaveJoe commented

C# .NET web.api json result get

I need to create an api and when invoked an endpoint to list the available players on http://localhost:3000/api/player/.

I should get json result like
[
{
"id": 1,
"name": "player name 1",
"position": "defender",
"playerSkills": [
{
"id": 1,
"skill": "defense",
"value": 60,
"playerId": 1
},
{
"id": 2,
"skill": "speed",
"value": 80,
"playerId": 1
}
]
},
{
"id": 2,
"name": "player name 2",
"position": "midfielder",
"playerSkills": [
{
"id": 3,
"skill": "attack",
"value": 20,
"playerId": 2
},
{
"id": 4,
"skill": "speed",
"value": 70,
"playerId": 2
}
]
}
]

I tried the below reference but this gives me output in xml


https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

dotnet-aspnet-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your question could span the entire .NET stack. Can you be more specific and provide the actual source code?

I tried the below reference but this gives me output in xml

Web API uses content negotiation which means the client gets to determine the result format. XML is the default in .NET Framework Web API and the results when using use a browser.

Content Negotiation in ASP.NET Web API


0 Votes 0 ·

1 Answer

KALYANAALLAM-4847 avatar image
0 Votes"
KALYANAALLAM-4847 answered AgaveJoe commented

@AgaveJoe

I added this in global.asax and it started working
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();


Product[] products = new Product[]
{
new Product { Id = 1, Name = "Player1", position="Midfielder" },
new Product { Id = 2, Name = "Player2",Position="Defender"}

     };

     public IEnumerable<Product> GetAllProducts()
     {
         return products;
     }

How do I add player skills to return the below json format please note player skills could be an array for a single Player.

[
{
"id": 1,
"name": "player name 1",
"position": "defender",
"playerSkills": [
{
"id": 1,
"skill": "defense",
"value": 60,
"playerId": 1
},
{
"id": 2,
"skill": "speed",
"value": 80,
"playerId": 1
}
]
},
{
"id": 2,
"name": "player name 2",
"position": "midfielder",
"playerSkills": [
{
"id": 3,
"skill": "attack",
"value": 20,
"playerId": 2
},
{
"id": 4,
"skill": "speed",
"value": 70,
"playerId": 2
}
]
}
]

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How do I add player skills to return the below json format please note player skills could be an array for a single Player.

The standard pattern is creating a data access layer that adds the player's skill by Id. Data is usually stored stored in a database. The community has no idea how your data is stored or how your data access works.

However, data access is a very common subject and covered in every beginning level Web API tutorial. The tutorial you shared above has a link Using Web API 2 with Entity Framework 6 which covers this subject.

0 Votes 0 ·