TruMan1 TruMan1 - 1 year ago 192
ASP.NET (C#) Question

How to return raw string with ApiController?

I have an ApiController that serves XML/JSON, but I would like one of my actions to return pure HTML. I tried the below but it still return XML/JSON.

public string Get()
return "<strong>test</strong>";

This is what the above returns:

<string xmlns="">&lt;strong&gt;test&lt;/strong&gt;</string>

Is there a way to return just the pure, unescaped text without even the surrounding XML tags (maybe a different return type of action attribute)?

Answer Source

You could have your Web Api action return an HttpResponseMessage for which you have full control over the Content. In your case you might use a StringContent and specify the correct content type:

public HttpResponseMessage Get()
    return new HttpResponseMessage()
        Content = new StringContent(
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download