- void no return value
- Ihttpactionresult
- Httpresponsemessage
- Custom Types
I don't want to repeat these return types here,
You can refer to the blog http://blog.csdn.net/leonken88/article/details/53063693
and the Great God's http://www.cnblogs.com/landeanfen/p/5501487.html
I'm going to say that the pit I met was,
Public Httpresponsemessage DataHandler (Requestmodel model)
{
Whether it is result = jsonconvert.serializeobject (data);
return result,
return json<string> (Result),
or return Ok (result)
Other people call my interface is a JSON string format, "{\" name\ ": \" 123\ "}"
}
In my mind, since you can receive this string, self-deserialization under OK, finally, the other side told me that their development language is C + +, and this HTTP request is a packaged component, similar to the type of DLL can only be called, they require to return JSON, must be {"name": "123"} , no escaping, matchless quotes
This is difficult, I tried a lot of ways, each time is returned with the escape character JSON string, helpless multi-lookup
[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?
The original is their own to the HTTP protocol learning is not fine,
1, the service side need to return a piece of ordinary text to the client, content-type= "Text/plain"
2, the server needs to return a piece of HTML code to the client, content-type= "text/html"
3, the server needs to return a piece of XML code to the client, content-type= "Text/xml"
4, the server needs to return a piece of JavaScript code to the client, Text/javascript
5, the server needs to return a Json string to the client, Application/json
And
the ASP. NET Webapi is a set of RESTful API development frameworks. The returned object is automatically serialized as XML or JSON, and its serialized format depends on the accept in the header of the request. common ones are:
- Application/json
- Application/xml
- Text/json
- Text/xml (both of which have been abandoned)
If not specified, the default returned Content-type is: Application/json; Charset=utf-8
To return the specified format to the HTTP response,
Method One: Discard automatic serialization, return httpresponsemessage, and then manually set
The code is as follows:
Httpresponsemessage resp = new Httpresponsemessage ();
String result= "{\" name\ ": \" 123\ "}";
Resp. Content = new Stringcontent (result, System.Text.Encoding.UTF8, "Text/plain");
Method Two: Set Httpconfiguration
But this method has a disadvantage, is the overall, but should be able to limit the scope, for the time being regardless.
Under P.S, IE does not display JSON by default, it will be downloaded directly, so the developer tools will not be able to capture the HTTP message or look at the header.
For more information, please refer to http://www.luckyonecn.com/blog/fix_content-type_to_Applicationjson_in_WebApi/
The "Knowledge point" HTTP request, not only the Content-type needs to specify in the request, response in the Content-type also need to specify, the client needs to tell the server, I give you what kind of data, Content-type, Server-side processing logic, return to the client, also need to tell the client, I give you what kind of data, content-type, both sides in the agreed-upon way to communicate, in order to ensure that the entire HTTP request is accurate.
(iv) pits in the ASP. NET Web API-"Return value of API"