且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Web API返回HttpResponseMessage的***方法

更新时间:2023-02-15 11:02:25

尽管这不能直接回答问题,但我想提供一些有用的信息. http: //weblogs.asp.net/dwahlin/archive/2013/11/11/new-features-in-asp-net-web-api-2-part-i.aspx

Although this is not directly answering the question, I wanted to provide some information I found usefull. http://weblogs.asp.net/dwahlin/archive/2013/11/11/new-features-in-asp-net-web-api-2-part-i.aspx

HttpResponseMessage或多或少已被IHttpActionResult取代.它更清洁,更易于使用.

The HttpResponseMessage has been more or less replaced with IHttpActionResult. It is much cleaner an easier to use.

public IHttpActionResult Get()
{
     Object obj = new Object();
     if (obj == null)
         return NotFound();
     return Ok(obj);
 }

然后,您可以封装以创建自定义的. 如何在使用IHttpActionResult时设置自定义标头?

Then you can encapsulate to create custom ones. How to set custom headers when using IHttpActionResult?

我还没有实现自定义结果的需要,但是当我这样做时,我会走这条路线.

I haven't found a need yet for implementing a custom result yet but when I do, I will be going this route.

它也可能与使用旧版本非常相似.

Its probably very similar to do using the old one as well.

对此进行进一步扩展并提供更多信息.您还可以在某些请求中包含消息.例如.

To expand further on this and provide a bit more info. You can also include messages with some of the requests. For instance.

return BadRequest("Custom Message Here");

您无法与其他许多邮件一起执行此操作,但可以帮助您发送回常见邮件.

You can't do this with many of the other ones but helps for common messages you want to send back.