asp.net的WebAPI控制器,​​输入返回实体或Htt的presponseMessage [英] asp.net webapi controller, return typed entity or HttpResponseMessage

查看:242
本文介绍了asp.net的WebAPI控制器,​​输入返回实体或Htt的presponseMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是使用的Htt presponseMessage在我ApiController返回类型的好处?比较直接返回类型化的实体或集合。

i would like to know what is the benefit of using HttpResponseMessage as the return type in my ApiController ? compare to returning the typed entity or collection directly.

我们试图在实践中决定把事情我们正在对这个项目保持一致。

we were trying to decided on a practice to keep things consistent for the project we are working on.

推荐答案

返回的Htt presponseMessage 当你试图使用您的控制器层的翻译是非常有用的HTTP协议和内部.NET服务之间。它允许通过HTTP有效载荷和头部直接控制。它可以很容易地返回202,204,304,303响应。它可以很容易地设置缓存头。您可以在响应的媒体类型显式控制。

Returning HttpResponseMessage is useful when you are trying to use your controller layer as the translation between the HTTP protocol and your internal .Net services. It allows direct control over the HTTP payload and headers. It makes it easy to return 202, 204, 304, 303 responses. It makes it easy to set caching headers. You have explicit control over the media type of the response.

通过返回一个对象,你有效地增加一个什么都不做层,你的架构。考虑....

By returning an object you effectively adding a "do nothing" layer to your architecture. Consider....

public Foo Get(int id) {
   return _fooRepository.GetFoo(id)
}

什么是这种方法的目的是什么?它增加什么价值?至少在MVC的土地中,控制器具有模型和视图匹配的作用

What is the purpose of this method? What value does it add? At least in MVC land, the controller had the role of matching the model and the view.

当你从间接使用一组特定于网络API / MVC和在HTTP世界上没有相应的概念抽象的冲击HTT presponseMessage的APIController返回的对象。格式化,ActionFilters,ModelBinders,Htt的presponseException都是基础架构,旨在允许的框架来处理您的HTTP请求和响应消息的幕后。

When you return objects from an APIController you have to impact the HTTPResponseMessage indirectly using a set of abstractions that are specific to Web API/MVC and have no corresponding concept in the HTTP world. Formatters, ActionFilters, ModelBinders, HttpResponseException are all infrastructure designed to allow the framework to process your HTTP request and response messages behind the scenes.

返回的Htt presponseMessage直接要求你的控制器方法做必要的工作,以返回所需的HTTP消息。

Returning HttpResponseMessage directly requires that your controller method does the work necessary to return the desired HTTP message.

我不相信它增加任何复杂到你的应用程序,它只是使发生了什么可见的。

I don't believe that it adds any complexity to your application, it just makes what is happening visible.

它归结为是否要使用的Web API作为一个对象远程通过HTTP框架(在这种情况下我也想看看ServiceStack),或者是否要利用HTTP作为一个应用协议。

It comes down to whether you want to use Web API as an "object remoting over HTTP" framework (in which case I would also take a look at ServiceStack) or whether you want to take advantage of HTTP as an application protocol.

这篇关于asp.net的WebAPI控制器,​​输入返回实体或Htt的presponseMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆