Put content in HttpResponseMessage object?
几个月前,微软决定改变httpResponseMessage类。以前,您可以简单地将数据类型传递到构造函数中,然后用该数据返回消息,但现在不能了。
现在,您需要使用Content属性来设置消息的内容。问题是它的类型是httpcontent,我似乎找不到将字符串转换为httpcontent的方法。
有人知道如何处理这个问题吗?谢谢。
对于字符串,最快的方法是使用StringContent构造函数
| 1
 | response.Content = new  StringContent("Your response text"); | 
对于其他常见场景,还有许多其他的httpcontent类后代。
		
		
- 有关创建自己的派生类型的StringContent(如JSON、XML等),请参阅下面的我的文章。
 
	 
您应该使用request.createResponse创建响应:
| 1
 | HttpResponseMessage response =  Request.CreateResponse(HttpStatusCode.BadRequest,"Error message"); | 
您可以将对象传递给createResponse,而不仅仅是字符串,它还将根据请求的accept头对其进行序列化。这样可以避免手动选择格式化程序。
		
		
- 这不只是在Web API中工作吗?
- 是的,这适用于继承apicontroller的控制器。
- 它自动与内容类型一起工作,因此您可以在不使用额外代码的情况下执行XML/JSON。
- 我认为,如果响应是错误的,那么调用CreateErrorResponse()更为正确,正如在这个答案的示例中所示。在我的Try-Catch中,我使用的是:this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,"message", exception);,如果您完全关心尊重呼叫者的accept header,而没有额外的恶作剧,那么这就是正确的答案。(您使用的是webapi)
- @弗洛伦迪米特瑞希的观点是,这只适用于你继承埃多克斯1(4)时。如果你只是继承了Controller,那么它就不起作用,你必须自己创造它:HttpResponseMessage msg = new HttpResponseMessage(); msg.Content = new StringContent("hi"); msg.StatusCode = HttpStatusCode.OK;。
 
	 
显然,新的方法在这里有详细说明:
http://aspnetwebstack.codeplex.com/discussions/350492
引用亨里克的话,
| 12
 3
 
 | HttpResponseMessage response = new  HttpResponseMessage(); 
response.Content = new  ObjectContent< T>( T, myFormatter,"application/some-format"); | 
因此,基本上,我们必须创建一个objectcontent类型,它显然可以作为httpcontent对象返回。
		
		
- 我的格式化程序是什么
- @用户1760329根据您的格式,它可能是一个new JsonMediaTypeFormatter();或类似的文件。
- 使用wcf找不到ObjectContent。
- 我不认为这是"新的方法"——你所引用的那篇文章将它列为一个备选方案,如果你想"完全控制你想要使用的[媒体类型]格式化程序"。
- 谢谢@praetor。这对我很有帮助
 
	 
对于任何T对象,您可以执行以下操作:
| 1
 | return Request.CreateResponse<T>(HttpStatusCode.OK, Tobject); | 
		
		
- 除Request外,只有在继承ApiController的情况下,CreateResponse方法才可用。如果使用Controller,它将不起作用。
 
	 
最简单的单线解决方案是使用
| 1
 | return new HttpResponseMessage(  HttpStatusCode.OK ) { Content =  new  StringContent("Your message here" ) }; | 
对于序列化JSON内容:
| 1
 | return new HttpResponseMessage(  HttpStatusCode.OK ) { Content =  new  StringContent(  SerializedString, System.Text.Encoding.UTF8 ,"application/json" ) }; | 
		
		
- 这对我不起作用,因为IHttpactionResult需要ResponseMessageResult的返回类型。请看下面我的答案,了解我的结局。另外,我还考虑了Nashawn的jsonContent(从StringContent基类派生)。
- 只需包装httpResponseMessage,然后:返回新的responseMessageResult(返回新的httpResponseMessage(httpStatusCode.ok)新的StringContent("此处显示您的消息"));:)
 
	 
您可以创建自己的专门内容类型。例如,一个用于JSON内容,一个用于XML内容(然后将其分配给httpResponseMessage.content):
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | 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")
 {
 }
 }
 | 
受Simon Mattes回答的启发,我需要满足IHttpactionResult Required返回类型的ResponseMessageResult。我也使用了纳肖恩的jsonContent,最后得到了…
| 12
 3
 4
 5
 
 |         return new System.Web.Http.Results.ResponseMessageResult(
            new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new  JsonContent( $"{JsonConvert.SerializeObject(contact, Formatting.Indented)}")
            }); | 
有关jsonContent,请参见Nashawn的答案。
毫无疑问,你是正确的弗洛林。我在做这个项目,发现这段代码:
| 1
 | product = await response.Content.ReadAsAsync<Product>(); | 
可替换为:
| 1
 | response.Content = new  StringContent(string  product); | 
		
		
- 此答案似乎与问题无关,也不显示如何从对象(产品)转到字符串。