0

On my server i have already serialized JSON to answer:

When I using Ok(string) it is serializing it again.

This is my function

public IHttpActionResult Logon([FromBody]Logon_Request model)
{
        string Answer= Get_Answer(model);

        return Ok(Answer); //not working
        return Ok(model);  //working fine
}

As the answer on the browser site instate of this

{
  "Token": "User token"
}

I have this

{\"Token\":\"User token\"}

Can you help me please:

-Can I prevent serialization?

-Do I need to use other types of return?

Michail
  • 366
  • 1
  • 3
  • 14
  • To return a string as-is, see duplicate [How to return raw string with ApiController?](http://stackoverflow.com/questions/14046417/how-to-return-raw-string-with-apicontroller). You shouldn't want to though, you generally want to let WebAPI handle the serialization: let the method `Get_Answer` return the answer _object_, not the serialized string. Then `return Ok(answer)` will work as expected. – CodeCaster Jul 23 '15 at 08:48
  • Thank you. Answer you point to is not ok: Error Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'System.Web.Http.IHttpActionResult'. And return object is not a case for me. – Michail Jul 23 '15 at 08:52
  • Then change the return type of your API method to `HttpResponseMessage` or see [Why should I use IHttpActionResult instead of HttpResponseMessage?](http://stackoverflow.com/questions/21758615/why-should-i-use-ihttpactionresult-instead-of-httpresponsemessage). – CodeCaster Jul 23 '15 at 08:54
  • Not quite sure what you mean. `public HttpResponseMessage Logon` will not show the compiler error you show if you use `return new HttpResponseMessage(...)`. – CodeCaster Jul 23 '15 at 08:59
  • It is ok. Was my mistake. It is working. Thank you. Should I delete answer? It is really duplicate. – Michail Jul 23 '15 at 09:00
  • No, just leave it, it will help more people find that answer by the wording of your question. :) Happy to help. – CodeCaster Jul 23 '15 at 09:00
  • One more question, please: if I want to use async version of Logon like this "public async Task Logon()" instate of my Logon function it is possible? – Michail Jul 23 '15 at 09:05
  • See [async Task Get VS HttpResponseMessage Get](http://stackoverflow.com/questions/25902275/async-taskhttpresponsemessage-get-vs-httpresponsemessage-get). If you use `Task` you're back to the root of your question, as WebAPI will handle the serialization again. – CodeCaster Jul 23 '15 at 09:07

0 Answers0