webapi2 return simple string without quotation mark

非 Y 不嫁゛ 提交于 2019-12-01 01:51:50

问题


Simple scenario:

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

returns:

"I am send by HTTP resonse"

Just curious, can I avoid the quotation mark in other words return:

I am send by HTTP resonse

or is this necessary in HTTP?


回答1:


Yes you can avoid the "

public class ValuesController : ApiController
{
        public string Get()
        {
            return "qwerty";
        }
}

now check http://localhost:3848/api/values

and response

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">qwerty</string>

The result with <string> tag but without quotes :)

EDIT

If you don't like this approach,try this.It returns just text

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;
}


来源:https://stackoverflow.com/questions/33688208/webapi2-return-simple-string-without-quotation-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!