【问题标题】:Return JSON as a response for HTTP Get in ASP.NET WebApi Controller在 ASP.NET WebApi 控制器中返回 JSON 作为 HTTP Get 的响应
【发布时间】:2015-02-26 09:59:36
【问题描述】:

我可以收集一些变量并列出并将其放入JSON中,如果HTTP Get调用成功将返回给客户端吗?

例如:

我有这个控制器,它必须返回帐户列表和更多值:

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 ?; 
    }

我可以将 userAccounts list、currentAccountsTotal、currentsAccountTotalCurrency、savingsAccountsTotal 放入一些 JSON 中返回给客户端吗?

我有调用规范,它看起来像这样:在 200 代码上,我将 JSON 中提到的所有内容返回给客户端。

在这种情况下我应该把什么作为返回值?

【问题讨论】:

  • 可以使用匿名变量吗?即:return Ok(new { userAccounts = userAccounts , something = foo1, somethingelse = foo2});
  • @Stefan 嗯,我会这样尝试 :) 感谢您的建议 :)

标签: c# asp.net json asp.net-web-api


【解决方案1】:

你需要知道的: 开箱即用的 webapi 支持内容协商的概念。

什么是内容协商? 内容协商是选择最佳表示(JSON、XML 等)的过程。

它是如何在 WebAPI 中完成的? 基本上,它正在读取接受头。

例子:

Accept: application/xml

如果 webapi 为该请求找到任何格式化程序,它将按照用户请求格式化响应。

您可以添加或删除格式化程序,例如,如果您希望始终使用 json,我们应该删除 xml 格式化程序,如下所示:

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

如果需要,您还可以创建自己的格式化程序并将其添加到配置中。

在代码中,您只需要根据您使用的返回类型返回您的对象或 Ok()。

在您的情况下,我们可以使用匿名对象,或者您可以请求自己的 DTO 来表示您的响应,这包括三个对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 2020-08-29
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多