WebAPI to Return XML

前端 未结 5 1755
挽巷
挽巷 2020-11-28 07:34

I\'m wanting my WEB API method to return an XML object back to the calling application. Currently it\'s just returning the XML as a string object. Is this a no no? If so how

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 07:53

    Here's another way to be compatible with an IHttpActionResult return type. In this case I am asking it to use the XML Serializer(optional) instead of Data Contract serializer, I'm using return ResponseMessage( so that I get a return compatible with IHttpActionResult:

    return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK)
           {
               Content = new ObjectContent(objectToSerialize, 
                  new System.Net.Http.Formatting.XmlMediaTypeFormatter { 
                      UseXmlSerializer = true 
                  })
           });
    

提交回复
热议问题