简体   繁体   中英

Return simple node in C# Rest Service

Sorry if this has already been asked but I couldn't find a solution anywhere. I've got a Rest service developped in C#.

For now, I've got a resource with a contract looking like this

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/tests)]
Test TestGet();

I'm migrating from an existing so I have to return the exact same XML than currently existing. Something like this :

<?xml version="1.0"?>
<test>OK</test>

I would like to return an object or a string but keep the result as an XML result and not a raw result. How could I do this ? If I return a String, I get this result :

<?xml version="1.0"?>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">KO</string>

If I return this object :

[DataContract]
public class Test
{
    [DataMember(Name = "Test")]
    public string Result { get; set; }
}

I get something like this :

<?xml version="1.0"?>
<Test xmlns="http://mynamespace">
    <Test>OK</Test>
</Test>

How could I return my answer in the expected format ? Do I have to return a raw text or can I do something having XML as response ?

Thanks for those who will try to help.

I just found how to do it. If someone has the same question there is the answer :

[DataContract(IsWrapped = false)]
public class Test
{
    [DataMember(Name = "Test")]
    public string Result { get; set; }
}

The IsWrapped attribute will prevent to wrap the body. It is kinda obvious now that I know it but it took me much time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM