在Web API中,从动作方法返回的类型通常是什么?
像这样返回CLR对象:
public IEnumerable<ContactModel> Get()
{
return _contactService.GetAllForUser();
}
或者将您的对象包装在一个HttpResponseMessage
中:
public HttpResponseMessage Get()
{
IEnumerable<ContactModel> contacts = _contactService.GetAllForUser();
return Request.CreateResponse((HttpStatusCode) 200, contacts);
}
我更喜欢将自己的CLR对象作为返回类型,因为这显然会导致更清晰的方法,因为您不必每次都进行HttpResponseMessage
实例化。