webapi2返回不带引号的简单字符串

cs0*_*815 7 c# asp.net-web-api asp.net-web-api2

简单方案:

public IHttpActionResult Get()
{
    return Ok<string>("I am send by HTTP resonse");
}
Run Code Online (Sandbox Code Playgroud)

返回:

"I am send by HTTP resonse"
Run Code Online (Sandbox Code Playgroud)

只是好奇,我可以避免用引号引起来吗?

I am send by HTTP resonse
Run Code Online (Sandbox Code Playgroud)

还是在HTTP中这是必需的?

Via*_*nev 9

是的,您可以避免使用“

public class ValuesController : ApiController
{
        public string Get()
        {
            return "qwerty";
        }
}
Run Code Online (Sandbox Code Playgroud)

现在检查 http://localhost:3848/api/values

和回应

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">qwerty</string>
Run Code Online (Sandbox Code Playgroud)

<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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了你的第一种方法,它返回了一个带引号的字符串。 (2认同)