简体   繁体   English

返回JSON作为ASP.NET WebApi控制器中HTTP Get的响应

[英]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? 我可以收集一些变量并列出并放入JSON中,如果HTTP Get调用成功,该变量将返回给客户端吗?

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? 我可以将userAccounts列表,currentAccountsTotal,currentsAccountTotalCurrency和SavingsAccountsTotal放入将返回到客户端的JSON中吗?

I have call specification and it looks like this: On 200 code I return all mentioned in JSON to client. 我有呼叫规范,它看起来像这样:在200个代码上,我将JSON中提到的所有内容返回给客户端。

在此处输入图片说明

What should I put as return value in this case? 在这种情况下,我应该把什么作为返回值?

What you need to know: Out of the box, webapi supports the notion of content negotiation. 您需要了解的内容:开箱即用,webapi支持内容协商的概念。

What is content negotiation? 什么是内容协商? Content negotiation is the process of selecting the best representation (JSON, XML, etc). 内容协商是选择最佳表示形式(JSON,XML等)的过程。

How is it done in WebAPI? 如何在WebAPI中完成? Basically, it is reading the accept header. 基本上,它正在读取accept标头。

Example: 例:

Accept: application/xml

If the webapi finds any formatter for that request, it will format the response as the user requested. 如果webapi找到该请求的任何格式化程序,它将根据用户请求格式化响应。

You can add or remove formatters, for example, if you want always json, we should remove the xml formatter, like this: 您可以添加或删除格式器,例如,如果要始终使用json,则应删除xml格式器,如下所示:

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. 在代码中,您只需要返回对象或Ok()即可,具体取决于您将什么用作返回类型。

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. 在您的情况下,我们可以使用匿名对象,也可以请求自己的DTO来表示您的响应,该响应将这三个对象放在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM