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="http://schemas.microsoft.com/2003/10/Serialization/"><strong>test</strong></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)?
I call the following webapi2 controller method from an mvc controller method:
I call it from this routine on the asp.net server:
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:We must strive not to return html but pure data from our API's and format data accordingly in the UI, but maybe you can use:
it works for me
Just
return Ok(value)
won't work, it will be treated asIEnumerable<char>
.Instead use
return Ok(new { Value = value })
or simillar.Another possible solution. In Web API 2 I used the base.Content() method of
APIController
:I needed to do this to get around an IE9 bug where it kept trying to download JSON content. This should also work for XML-type data by using the
XmlMediaTypeFormatter
media formatter.Hope that helps someone.
If you are using MVC rather than WebAPI you can use the base.Content method: