C# 将内容放入 HttpResponseMessage 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12240713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Put content in HttpResponseMessage object?
提问by praetor
Several months ago, Microsoft decided to change up the HttpResponseMessage class. Before, you could simply pass a data type into the constructor, and then return the message with that data, but not anymore.
几个月前,Microsoft 决定更改 HttpResponseMessage 类。以前,您可以简单地将数据类型传递给构造函数,然后返回带有该数据的消息,但现在不能了。
Now, you need to use the Content property to set the content of the message. The problem is that it is of type HttpContent, and I can't seem to find a way to convert a string, for example, to HttpContent.
现在,您需要使用 Content 属性来设置消息的内容。问题是它是 HttpContent 类型,我似乎找不到将字符串转换为 HttpContent 的方法。
Does anyone know how to deal with this issue? Thanks a lot.
有谁知道如何处理这个问题?非常感谢。
采纳答案by praetor
Apparently the new way to do it is detailed here:
显然,这里详细介绍了新的方法:
http://aspnetwebstack.codeplex.com/discussions/350492
http://aspnetwebstack.codeplex.com/discussions/350492
To quote Henrik,
引用亨利克的话,
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ObjectContent<T>(T, myFormatter, "application/some-format");
So basically, one has to create a ObjectContent type, which apparently can be returned as an HttpContent object.
所以基本上,必须创建一个 ObjectContent 类型,它显然可以作为 HttpContent 对象返回。
回答by Jim O'Neil
For a string specifically, the quickest way is to use the StringContentconstructor
特别是对于字符串,最快的方法是使用StringContent构造函数
response.Content = new StringContent("Your response text");
There are a number of additional HttpContent class descendantsfor other common scenarios.
对于其他常见场景,还有许多额外的HttpContent 类后代。
回答by Florin Dumitrescu
You should create the response using Request.CreateResponse:
您应该使用Request.CreateResponse创建响应:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Error message");
You can pass objects not just strings to CreateResponse and it will serialize them based on the request's Accept header. This saves you from manually choosing a formatter.
您可以将对象不仅仅是字符串传递给 CreateResponse,它会根据请求的 Accept 标头序列化它们。这使您无需手动选择格式化程序。
回答by ScubaSteve
For any T object you can do:
对于任何 T 对象,您可以执行以下操作:
return Request.CreateResponse<T>(HttpStatusCode.OK, Tobject);
回答by bytedev
You can create your own specialised content types. For example one for Json content and one for Xml content (then just assign them to the HttpResponseMessage.Content):
您可以创建自己的专用内容类型。例如,一个用于 Json 内容,一个用于 Xml 内容(然后将它们分配给 HttpResponseMessage.Content):
public class JsonContent : StringContent
{
public JsonContent(string content)
: this(content, Encoding.UTF8)
{
}
public JsonContent(string content, Encoding encoding)
: base(content, encoding, "application/json")
{
}
}
public class XmlContent : StringContent
{
public XmlContent(string content)
: this(content, Encoding.UTF8)
{
}
public XmlContent(string content, Encoding encoding)
: base(content, encoding, "application/xml")
{
}
}
回答by Simon Mattes
The easiest single-line solution is to use
最简单的单行解决方案是使用
return new HttpResponseMessage( HttpStatusCode.OK ) {Content = new StringContent( "Your message here" ) };
For serialized JSON content:
对于序列化的 JSON 内容:
return new HttpResponseMessage( HttpStatusCode.OK ) {Content = new StringContent( SerializedString, System.Text.Encoding.UTF8, "application/json" ) };
回答by Adam Cox
Inspired by Simon Mattes' answer, I needed to satisfy IHttpActionResult required return type of ResponseMessageResult. Also using nashawn's JsonContent, I ended up with...
受 Simon Mattes 回答的启发,我需要满足 IHttpActionResult 所需的 ResponseMessageResult 返回类型。同样使用 nashawn 的 JsonContent,我最终得到了......
return new System.Web.Http.Results.ResponseMessageResult(
new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new JsonContent(JsonConvert.SerializeObject(contact, Formatting.Indented))
});
See nashawn's answer for JsonContent.
请参阅 nashawn 对 JsonContent 的回答。
回答by Steven W. H.
No doubt that you are correct Florin. I was working on this project, and found that this piece of code:
毫无疑问,你是对的,弗洛林。我在做这个项目,发现这段代码:
product = await response.Content.ReadAsAsync<Product>();
Could be replaced with:
可以替换为:
response.Content = new StringContent(string product);

