当遇到API时,我通常会在我从IHttpActionResult接口(例如OK())返回方法的位置写入它们,并将返回的对象作为OK()方法中的参数放置。或者我可以返回从数据库中检索到的对象。返回IHttpActionResult和返回对象之间的区别
示例代码:使用IHttpActionResult
[Route("GetAll1")]
[HttpGet]
public IHttpActionResult GetAll1()
{
List<ContactWebLink> contactWebLinks = new List<ContactWebLink>();
try
{
Manager<ContactWebLink> contactWebLinkManager = new Manager<ContactWebLink>(Unit);
contactWebLinks = contactWebLinkManager.FindAll(a => a.IsDeleted == false, null, null, null);
return Ok(contactWebLinks);
}
catch
{
return InternalServerError();
}
finally
{
contactWebLinks = null;
}
}
Vs的使用对象的类,我回到
[Route("GetAll2")]
[HttpGet]
public List<ContactWebLink> GetAll2()
{
List<ContactWebLink> contactWebLinks = new List<ContactWebLink>();
try
{
Manager<ContactWebLink> contactWebLinkManager = new Manager<ContactWebLink>(Unit);
contactWebLinks = contactWebLinkManager.FindAll(a => a.IsDeleted == false, null, null, null);
return contactWebLinks;
}
catch
{
return null;
}
finally
{
contactWebLinks = null;
}
}
使用邮差测试的结果,我看到任何区别。两者都会产生200个响应代码(当没有发生异常时)。两者都返回相同的结果(我从数据库中检索的实体)。
那么使用这两种方法之间的区别,以及我应该什么时候使用两种方法之间的区别?
这是我使用IHttpActionReslt时获得的唯一好处吗? –
在我看来,这是一个很大的好处 – Alex
但都返回适当的HTTP状态码。 –