Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API 2 return simple string without quotation mark when return type is IHttpActionResult

My question is referring to this question.

The answer shows only solutions where you would have to change the type of the method to HttpResponseMessage or string.

This is my method:

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

It returns:

"I am send by HTTP resonse"

I expect:

I am send by HTTP response

Is there a way to return a simple string without quotation mark where the return type of the method is IHttpActionResult?

like image 543
Nightscape Avatar asked Mar 22 '26 21:03

Nightscape


1 Answers

You can use ApiController.ResponseMessage(HttpResponseMessage) Method

Creates a ResponseMessageResult with the specified response.

ResponseMessageResult is derived from IHttpActionResult

public IHttpActionResult Get() {
    var message = "I am send by HTTP response";
    var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) {
        Content = new StringContent(message, System.Text.Encoding.UTF8, "text/plain")
    };
    return ResponseMessage(httpResponseMessage);
}
like image 80
Nkosi Avatar answered Mar 24 '26 11:03

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!