nemo_87
nemo_87

Reputation: 4781

Return JSON as a response for HTTP Get in ASP.NET WebApi Controller

Can I collect few variables and list and put it into JSON, that will be returned to client if HTTP Get call is successful?

For example:

I have this Controller that has to return list of accounts, and few more values:

public class BankAccountController : ApiController
{
    [Authorize]
    [Route("/accounts")]
    public IHttpActionResult GetAccounts()
    {

        List<Account> userAccounts = new List<Account>{
              new Account { 
                AccountNumber = 1,
                Available = 2346.220m,
                Balance = 3219.12m,
                Currency = "euro",
                InterestRate = 1,
                Name = "Current account",
                Type = ""},

             new Account {
                AccountNumber = 2,
                Available = 12346.220m,
                Balance = 32219.12m,
                Currency = "euro",
                InterestRate = 3,
                Name = "Saving account",
                Type = ""},

             new Account {
                AccountNumber = 3,
                Available = 346.220m,
                Balance = 219.12m,
                Currency = "euro",
                InterestRate = 3,
                Name = "Current account",
                Type = ""},

             new Account {
                AccountNumber = 4,
                Available = 37846.220m,
                Balance = 21943.12m,
                Currency = "euro",
                InterestRate = 3,
                Name = "Saving account",
                Type = ""},

             new Account {
                AccountNumber = 5,
                Available = 137846.220m,
                Balance = 21943.12m,
                Currency = "euro",
                InterestRate = 3,
                Name = "Saving account",
                Type = ""},

             new Account {
                AccountNumber = 6,
                Available = 7846.220m,
                Balance = 21943.12m,
                Currency = "euro",
                InterestRate = 3,
                Name = "Current account",
                Type = ""} 
            };

        var currentAccountsTotal = userAccounts.Count();
        string currentsAccountTotalCurrency = "something";
        string savingsAccountTotalCurrency = "something";
        decimal savingsAccountsTotal = userAccounts.Where(a => a.Name == "Saving account").Select(b => b.Balance).Sum();
        return ?; 
    }

Can I take userAccounts list, currentAccountsTotal, currentsAccountTotalCurrency , savingsAccountsTotal and put them into some JSON that will be returned to client?

I have call specification and it looks like this: On 200 code I return all mentioned in JSON to client.

enter image description here

What should I put as return value in this case?

Upvotes: 0

Views: 1965

Answers (1)

Bruno Costa
Bruno Costa

Reputation: 2720

What you need to know: Out of the box, webapi supports the notion of content negotiation.

What is content negotiation? Content negotiation is the process of selecting the best representation (JSON, XML, etc).

How is it done in WebAPI? Basically, it is reading the accept header.

Example:

Accept: application/xml

If the webapi finds any formatter for that request, it will format the response as the user requested.

You can add or remove formatters, for example, if you want always json, we should remove the xml formatter, like this:

config.Formatters.Remove(config.Formatters.XmlFormatter);

You can also create your own formatter if you need and add it to the configuration.

At the code, you only need to return your object or Ok() depending on what are you using as return type.

In your case, we can use a anonymous object or you can request your own DTO to represent your response, that includes the three objects together.

Upvotes: 2

Related Questions