返回JSON对象(ASP。净WebAPI)

时间:2022-02-11 21:31:27

I have ASP.NET Web API

我有ASP。净Web API

It returns me JSON like this

它像这样返回JSON

[{"CompanyID":1,"CompanyName":"Тест"},{"CompanyID":5,"CompanyName":"Фокстрот"}]

[{“CompanyID”:1、“公司名称”:“Тест”},{“CompanyID”:5,“公司名称”:“Фокстрот”}]

As I understood this is Json array, but I need to return JSOn Object instead of it

正如我所理解的,这是Json数组,但是我需要返回Json对象而不是它

Like this: {"results":[{"CompanyID":1,"CompanyName":"Тест"},{"CompanyID":5,"CompanyName":"Фокстрот"}]}

这样的:{“结果”:[{“CompanyID”:1、“公司名称”:“Тест”},{“CompanyID”:5,“公司名称”:“Фокстрот”}]}

Here is my GetCompanies controller:

这是我的GetCompanies控制器:

public class GetCompaniesController : ApiController
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: api/GetCompanies
    public IQueryable<Companies> GetCompanies()
    {
        return db.Companies;
    }

    // GET: api/GetCompanies/5
    [ResponseType(typeof(Companies))]
    public async Task<IHttpActionResult> GetCompanies(int id)
    {
        Companies companies = await db.Companies.FindAsync(id);
        if (companies == null)
        {
            return NotFound();
        }

        return Ok(companies);
    }

    // PUT: api/GetCompanies/5
    [ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutCompanies(int id, Companies companies)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != companies.CompanyID)
        {
            return BadRequest();
        }

        db.Entry(companies).State = EntityState.Modified;

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!CompaniesExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/GetCompanies
    [ResponseType(typeof(Companies))]
    public async Task<IHttpActionResult> PostCompanies(Companies companies)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Companies.Add(companies);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = companies.CompanyID }, companies);
    }

    // DELETE: api/GetCompanies/5
    [ResponseType(typeof(Companies))]
    public async Task<IHttpActionResult> DeleteCompanies(int id)
    {
        Companies companies = await db.Companies.FindAsync(id);
        if (companies == null)
        {
            return NotFound();
        }

        db.Companies.Remove(companies);
        await db.SaveChangesAsync();

        return Ok(companies);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool CompaniesExists(int id)
    {
        return db.Companies.Count(e => e.CompanyID == id) > 0;
    }
}

How I need to modify my controller?

我需要如何修改我的控制器?

Thank's

谢谢的

1 个解决方案

#1


18  

When returning a value, try it like this:

当返回一个值时,可以这样尝试:

      public IHttpActionResult GetCompanies()
      {
          var companies = db.Companies.ToList();
          return Ok( new { results = companies });
      }

#1


18  

When returning a value, try it like this:

当返回一个值时,可以这样尝试:

      public IHttpActionResult GetCompanies()
      {
          var companies = db.Companies.ToList();
          return Ok( new { results = companies });
      }