我刚刚开始学习ASP.NET Web API,并且我创建了一个带有repo的Person模型,该模型将该模型类型的集合存储在内存中以供测试。
当我调用我的/ api /值时,应该返回一个List但是我得到的只是“这个XML文件似乎没有与之关联的任何样式信息。文档树如下所示。
<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebApiExample.Models"/>"
值控制器的代码:
public IEnumerable<Person> Get()
{
return personRepo.GetPeople();
}
PersonRepository的代码片段:
public IEnumerable<Person> GetPeople()
{
return context.Person.ToList();
}
开发者工具从IE中捕获url / api / values,Reponse Body = []
但是,当我指定一个int时它可以工作,例如/ API /值/ 3
{"Id":3,"Name":"Sarah","Age":54,"Gender":"Fenale","IsMarried":true}
任何线索?
REPO:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace WebApiExample.Models
{
public class PersonRepository : IPersonRepository, IDisposable
{
private PersonContext context;
public PersonRepository(PersonContext context)
{
this.context = context;
}
public void DeletePerson(int Id)
{
Person person = context.Person.Find(Id);
context.Person.Remove(person);
}
public IEnumerable<Person> GetPeople()
{
return context.Person.ToList();
}
public Person GetPersonById(int Id)
{
return context.Person.Find(Id);
}
public void InsertPerson(Person person)
{
context.Person.Add(person);
}
public void UpdatePerson(Person person)
{
context.Entry(person).State = System.Data.Entity.EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
人物背景:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApiExample.Models
{
public class PersonContext : DbContext
{
public DbSet<Person> Person { get; set; }
}
}
值控制器:
using System.Collections.Generic;
using WebApiExample.Models;
using System.Web.Http;
namespace WebApiExample.Controllers
{
[AllowAnonymous]
public class ValuesController : ApiController
{
private IPersonRepository personRepo;
public ValuesController()
{
this.personRepo = new PersonRepository(new PersonContext());
Person person = new Person()
{
Id = 1,
Name = "Joanna",
Age = 28,
Gender = "Female",
IsMarried = true
};
Person person1 = new Person()
{
Id = 2,
Name = "Jenna",
Age = 52,
Gender = "Female",
IsMarried = true
};
Person person2 = new Person()
{
Id = 3,
Name = "John",
Age = 54,
Gender = "Male",
IsMarried = true
};
Person person3 = new Person()
{
Id = 4,
Name = "Jessop",
Age = 28,
Gender = "Male",
IsMarried = true
};
Person person4 = new Person()
{
Id = 5,
Name = "Rachael",
Age = 24,
Gender = "Female",
IsMarried = false
};
personRepo.InsertPerson(person);
personRepo.InsertPerson(person1);
personRepo.InsertPerson(person2);
personRepo.InsertPerson(person3);
personRepo.InsertPerson(person4);
}
// GET api/values
public IEnumerable<Person> Get()
{
return personRepo.GetPeople();
}
// GET api/values/5
public Person Get(int id)
{
return personRepo.GetPersonById(id);
}
// POST api/values
public void Post([FromBody]Person person)
{
personRepo.InsertPerson(person);
}
// PUT api/values/5
public void Put(int id, [FromBody]Person value)
{
Person person = personRepo.GetPersonById(id);
personRepo.UpdatePerson(person);
}
// DELETE api/values/5
public void Delete(int id)
{
personRepo.DeletePerson(id);
}
}
}
接口:
using System;
using System.Collections.Generic;
namespace WebApiExample.Models
{
interface IPersonRepository : IDisposable
{
IEnumerable<Person> GetPeople();
Person GetPersonById(int Id);
void InsertPerson(Person person);
void DeletePerson(int Id);
void UpdatePerson(Person person);
void Save();
}
}