简单方案:
public IHttpActionResult Get()
{
return Ok<string>("I am send by HTTP resonse");
}
返回:
"I am send by HTTP resonse"
只是好奇,我可以避免使用引号返回:
I am send by HTTP resonse
或在HTTP中是否必要?
答案 0 :(得分:6)
是的,你可以避免“
public class ValuesController : ApiController
{
public string Get()
{
return "qwerty";
}
}
现在检查http://localhost:3848/api/values
和回复
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">qwerty</string>
带有<string>
标记但没有引号的结果:)
修改强>
如果您不喜欢这种方法,请尝试此操作。它只返回文本
public HttpResponseMessage Get()
{
string result = "Your text";
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return resp;
}