Put content in HttpResponseMessage object?

后端 未结 8 1535
感情败类
感情败类 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

    The easiest single-line solution is to use

    return new HttpResponseMessage( HttpStatusCode.OK ) {Content =  new StringContent( "Your message here" ) };
    

    For serialized JSON content:

    return new HttpResponseMessage( HttpStatusCode.OK ) {Content =  new StringContent( SerializedString, System.Text.Encoding.UTF8, "application/json" ) };
    

提交回复
热议问题