Request.CreateResponse()的等价

时间:2014-02-24 19:27:04

标签: c# asp.net-web-api httpresponse

等同于

var newOrder = new OrderViewModel()
{

   Id = orderDetails.Id,
   Name = orderDetails.Service.Name,
   .....
};

Request.CreateResponse(HttpStatusCode.OK, newOrder);

采用以下格式:

HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.ReasonPhrase = "successfully created blablabla";

Request.CreateResponse()我可以传递一个对象。我怎么能用HttpResponseMessage

来做到这一点

希望能够传递消息(例如ReasonPhrase),但不能通过Request.CreateResponse()传递消息。或者我错过了什么。

1 个答案:

答案 0 :(得分:2)

要传入对象作为响应内容,您可以构建ObjectContent<T>

response.Content = new ObjectContent<OrderViewModel>(newOrder, 
                                         new JsonMediaTypeFormatter())

ObjectContent的其他构造函数:

http://msdn.microsoft.com/en-us/library/hh835040(v=vs.118).aspx