如何使用ApiController返回原始字符串? [英] How to return raw string with ApiController?
问题描述
我有供应XML / JSON的ApiController,但我想我的行动来回报纯HTML之一。我想下面的,但它仍然返回XML / JSON。
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>";
}
这是上面的回报:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><strong>test</strong></string>
有没有办法,只是返回,甚至没有周围的XML标记纯,未转义文本(也许action属性不同的返回类型)?
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)?
推荐答案
您可以让你的网页API操作返回的Htt presponseMessage
您拥有完全控制权以上内容。在你的情况,你可能使用的StringContent并指定正确的内容类型:
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(
"<strong>test</strong>",
Encoding.UTF8,
"text/html"
)
};
}
这篇关于如何使用ApiController返回原始字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!