Put content in HttpResponseMessage object?

后端 未结 8 1483
感情败类
感情败类 2020-11-28 03:31

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

相关标签:
8条回答
  • 2020-11-28 03:57

    For any T object you can do:

    return Request.CreateResponse<T>(HttpStatusCode.OK, Tobject);
    
    0 讨论(0)
  • 2020-11-28 04:05

    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):

    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")
        {
        }
    }
    
    0 讨论(0)
提交回复
热议问题