为什么我要使用IHttpActionResult代替了Htt presponseMessage? [英] Why should I use IHttpActionResult instead of HttpResponseMessage?
问题描述
我一直的WebAPI开发,并已转移到WebApi2,微软已经推出,这似乎建议在返回<$ C $要使用的新的 IHttpActionResult
接口C>的Htt presponseMessage 。我对这个新界面的优势困惑。这似乎主要是公正提供的略更简单的方法来创建一个的Htt presponseMessage
。
我会做的说法,这是抽象的抽象的缘故。我缺少的东西吗?什么是现实世界中的优势我使用这个新的接口,除了节省可能的线路code的获得?
老办法(适用的WebAPI):
公开的Htt presponseMessage删除(INT ID)
{
VAR状态= _Repository.DeleteCustomer(ID);
如果(状态)
{
返回新的Htt presponseMessage(的HTTPStatus code.OK);
}
其他
{
抛出新的Htt presponseException(的HTTPStatus code.NotFound);
}
}
新途径(WebApi2):
公共IHttpActionResult删除(INT ID)
{
VAR状态= _Repository.DeleteCustomer(ID);
如果(状态)
{
//返回新的Htt presponseMessage(的HTTPStatus code.OK);
返回OK();
}
其他
{
//抛出新的Htt presponseException(的HTTPStatus code.NotFound);
返回NOTFOUND();
}
}
您可能决定不使用 IHttpActionResult
,因为现有的code建立了一个的Htt presponseMessage
不符合罐头措施之一。可以使用 ResponseMessage <的罐头响应但是适应
的Htt presponseMessage
到 IHttpActionResult
/ code>。我花了一段时间才弄清楚这一点,所以我想将它张贴表明你不necesarily必须选择一个或另一个:
公共IHttpActionResult SomeAction()
{
IHttpActionResult响应;
//我们希望有一个303来设置位置的能力
HTT presponseMessage responseMsg =新HTT presponseMessage(的HTTPStatus code.RedirectMethod);
responseMsg.Headers.Location =新的URI(HTTP://customLocation.blah);
响应= ResponseMessage(responseMsg);
返回响应;
}
请注意, ResponseMessage
是基类的方法 ApiController
您的控制器应该继承。
I have been developing with WebApi and have moved on to WebApi2 where Microsoft has introduced a new IHttpActionResult
Interface that seems to recommended to be used over returning a HttpResponseMessage
. I am confused on the advantages of this new Interface. It seems to mainly just provide a SLIGHTLY easier way to create a HttpResponseMessage
.
I would make the argument that this is "abstraction for the sake of abstraction". Am I missing something? What is the real world advantages I get from using this new Interface besides maybe saving a line of code?
Old way (WebApi):
public HttpResponseMessage Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
New Way (WebApi2):
public IHttpActionResult Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
//return new HttpResponseMessage(HttpStatusCode.OK);
return Ok();
}
else
{
//throw new HttpResponseException(HttpStatusCode.NotFound);
return NotFound();
}
}
You might decide not to use IHttpActionResult
because your existing code builds a HttpResponseMessage
that doesn't fit one of the canned responses. You can however adapt HttpResponseMessage
to IHttpActionResult
using the canned response of ResponseMessage
. It took me awhile to figure this out, so I wanted to post it showing that you don't necesarily have to choose one or the other:
public IHttpActionResult SomeAction()
{
IHttpActionResult response;
//we want a 303 with the ability to set location
HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
responseMsg.Headers.Location = new Uri("http://customLocation.blah");
response = ResponseMessage(responseMsg);
return response;
}
Note, ResponseMessage
is a method of the base class ApiController
that your controller should inherit from.
这篇关于为什么我要使用IHttpActionResult代替了Htt presponseMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!