(D) Asp.net web api in the pit - the return value [api]

  • None Return Value void
  • IHttpActionResult
  • HttpResponseMessage
  • Custom Types

I do not want to repeat them here return type,

You can refer Bowen http://blog.csdn.net/leonken88/article/details/53063693

And the god of http://www.cnblogs.com/landeanfen/p/5501487.html

I have to say I met pit is

public HttpResponseMessage DataHandler(RequestModel model)
{

Whether result = JsonConvert.SerializeObject (data);

return result,

return json<string>(result),

Or return Ok (result)

Others call my interfaces are a json format string, "{\" name \ ": \" 123 \ "}"

}

I would like to do, since you can receive this string, under their own anti-serialization on ok Well, finally another over there told me that their development language is c ++, and the http requests are packaged components, similar dll that can only be called, they asked to return json, must be { "name": "123"}, no escape, without double quotes

This can be difficult and I tried a good variety of ways, each time returned json string with the escape character, but unfortunately multiparty review

[HttpPost]
public HttpResponseMessage DataHandler(RequestModel model)
{

     string result=”{\“name\”:\”123\”}”;

     return new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain") };

}

That's right, why?

That their own learning http protocol is not fine,

1, the server needs to return some plain text to the client, the Content-the Type = "text / Plain"
2, the server needs to return HTML code to the client, the Content-the Type = "text / HTML"
. 3, the server needs to return section of XML code to the client, the Content-the Type = "text / XML"
. 4, the server needs to return to the client some javascript code, text / javascript
. 5, the server needs to return some json string to the client, application / json

And

ASP.NET WebAPI RESTful API is a set of development framework. Object serialization automatically returned as XML or JSON, which depends on the serialized format in the header Request Accept. Common are:

  • application/json
  • application/xml
  • text/json
  • text / xml (both have been abandoned)

If not specified, the default is to return the Content-Type: application / json; charset = utf-8

To http response returns the specified format,

Method a: abandon automatic serialization return httpResponseMessage, manually set

code show as below:

HttpResponseMessage resp = new HttpResponseMessage();

string result=”{\“name\”:\”123\”}”;

resp.Content = new StringContent(result, System.Text.Encoding.UTF8, ""text/plain");

Method two: Set httpConfiguration

image

However, this method has drawbacks, is global, but should be able to define the scope, being the first to go on.

Under PS Tucao, IE default JSON is not displayed, it will be downloaded directly down, so the developer tools will not capture HTTP packets, it also can not see Header.

For more details, please refer http://www.luckyonecn.com/blog/fix_content-type_to_Applicationjson_in_WebApi/

 

[Knowledge] http request, the request is not only need to specify the content-type, content-type response is also need to specify the client need to tell the server, I'll give you what kind of data, content-type, service end processing logic, returned to the client, but also need to tell the client, I'll give you what kind of data, content-type, according to the agreement the two sides a good way to communicate, to ensure accurate conduct throughout the http request.

Guess you like

Origin www.cnblogs.com/lhxsoft/p/11887104.html