Talk about the use of Web APIs in ASP.

Source: Internet
Author: User

nonsense

with the rise of app applications and a growing number of backend services, Microsoft will certainly provide a more convenient way to develop in this area, in addition to many excellent nodejs frameworks. This is the usual style of Microsoft, if from the development of the convenience of Microsoft is a well-deserved big brother, just fish and bear paw can not be combined, a variety of grammatical sugar to bring the development of convenience will inevitably sacrifice the performance of the program run, which is inevitable, general and efficiency is always the need for programmers to choose the topic. It's over, it's over. Today's protagonist the Web Api,web API is a technology that appears after mvc4.0, and the convenience of his creation and bringing it to me is not introduced. This article mainly talk about, I have some understanding of Web API development.

Topic one: [Frombody] Attribute modifiers

Frombody is a modifier parameter, such as: public void Post ([frombody]string name), then what is his role, in fact, his role is to query the Post request parameter is the value of name.

Note that: [Frombody] can only modify a single parameter, if it is more than one parameter, it will be encapsulated into an object for data-specific delivery, such as: public void Post ([frombody]string name,[frombody]int ID) is not a value, the ID and name are encapsulated into a Userinfo object and invoked using public void Post ([frombody]userinfo user).

Topic Two: Request Mode (Get & Post)

keyword distinction: Get requests or post requests can be distinguished by their own keywords, such as: GetUser (int id) is definitely a GET request,Getxxx (params param) format is a GET request , the same as post. Of course, there is no keyword modification method, such as: Finduser in this case, the default is the request for a POST request GET request will be reported 405 error.

Topic Three: Returning results in JSON format

This will simply set the return value of net to the JSON format, you can return Jobject or Jarray can be used Ajax to receive JSON objects. The code is as follows:

 Public jobject postfinduser ([frombody]userinfo user) {    new  jobject ();    res["name"string. Format ("{0} is a good person ~", user. Name);     return Res;}

Talk about the use of Web APIs in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.