我在這上面呆了幾個小時。只希望json在使用WebApi 2和EntityFramework時返回
我正在使用WebApi 2和Entity Framework 6.1.3。我正在學習本教程: https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/
我只想返回json。當我打到url http://localhost:11440/Api/Contacts。我收到以下錯誤: 'ObjectContent`1'類型無法序列化內容類型'application/xml;字符集= UTF-8' 。
我有以下的模型和控制器進行定義:
Address.cs
namespace Blah_Application.Models
{
public class Address
{
public int AddressID { get; set; }
public int ContactID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual Contact Contact { get; set; }
}
}
Contact.cs
using System.Collections.Generic;
namespace Blah_Application.Models
{
public class Contact
{
public Contact()
{
Phones = new HashSet<Phone>();
Addresses = new HashSet<Address>();
}
public int ContactID { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public bool Favorite { get; set; }
public string SmallImageUrl { get; set; }
public string LargeImageUrl { get; set; }
public string Email { get; set; }
public string Website { get; set; }
public string BirthDate { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
}
Phone.cs
namespace Ecitslos_Application.Models
{
public enum PhoneType { Home, Work, Mobile}
public class Phone
{
public int PhoneID {get; set;}
public int ContactID { get; set; }
public PhoneType PhoneType { get; set; }
public string Number { get; set; }
public virtual Contact Contact { get; set; }
}
}
ContactsController.cs
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using Blah_Application.Models;
namespace Ecitslos_Application.Controllers
{
public class ContactsController : ApiController
{
private Ecitslos_ApplicationContext db = new
Ecitslos_ApplicationContext();
// GET: api/Contacts
public IQueryable<Contact> GetContacts()
{
return db.Contacts;
}
// GET: api/Contacts/5
[ResponseType(typeof(Contact))]
public IHttpActionResult GetContact(int id)
{
Contact contact = db.Contacts.Find(id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
}
// PUT: api/Contacts/5
[ResponseType(typeof(void))]
public IHttpActionResult PutContact(int id, Contact contact)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != contact.ContactID)
{
return BadRequest();
}
db.Entry(contact).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ContactExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Contacts
[ResponseType(typeof(Contact))]
public IHttpActionResult PostContact(Contact contact)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Contacts.Add(contact);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = contact.ContactID }, contact);
}
// DELETE: api/Contacts/5
[ResponseType(typeof(Contact))]
public IHttpActionResult DeleteContact(int id)
{
Contact contact = db.Contacts.Find(id);
if (contact == null)
{
return NotFound();
}
db.Contacts.Remove(contact);
db.SaveChanges();
return Ok(contact);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool ContactExists(int id)
{
return db.Contacts.Count(e => e.ContactID == id) > 0;
}
}
}
這不適合我。當我嘗試我收到錯誤:自檢屬性'聯繫'檢測到類型'System.Data.Entity.DynamicProxies.Contact_F0D869DA35A5D79D9FC7459ECA71E46F93E7CACF4EB08F9DCCF7A7DCCF99E1D3 – hikerclimb
@hikerclimb這是一個單獨的問題,只返回JSON。我編輯了我的答案,以包含第二個配置設置來處理該問題。 – user2880616
@hikerclimb是因爲你在'Phone'中有'Contact',然後你在'Contact'中實例化'Phone'? –