Webapi-Chuan-shen (I.)

Source: Internet
Author: User

At present, their work and WEBAPI related, unavoidable incoming, receive parameters. The old way is to get JSON from the request stream, and then deserialize, there are 2 uncontrollable places, one is a stream, one is deserialization, all need to try, always feel very uncomfortable. Therefore, we have done in-depth research (DU) on Webapi.

The normal WEBAPI has three ways to receive parameters:

1. In the form of a JSON string. Gets the incoming JSON string from the requested input stream, and then deserializes it into the entity it needs. The use of Request.inputstream.

2, in the form of URL to pass in. The WEBAPI is automatically converted into the corresponding entity. Similar to the submission of form forms, such as Name=aaa&pwd=bbb&token=xxx, requires the contenttype= of the request header "application/x-www-form-urlencoded

3, in the form of a JSON string, Webapi automatically converted to the corresponding entity. Contenttype= "Application/json" is required for the request header

Start with the simplest string. Crawl through the pits.

Web-side

 Public class Valuescontroller:apicontroller    {        [httppost]        publicstring GetData (string  name)        {             return " I love " + name;        }    }

HTML-side JS

            $ (' #btn '). Click (function () {                $.ajax ({                    type: "Post",                    URL: "Http://localhost:7601/api/values/GetData",                    data: "Liu Yifei",                    success:function (r) {                        alert (r);                    }                );            });

The result met the first pit .

  

Conclusion: Http://localhost:7601/api/values/GetData and Http://localhost:7601/api/values/GetData?name= Liu Yifei are two different URLs

Modify the code in JS above
            $ (' #btn '). Click (function () {                $.ajax ({                    type: "Post",                    URL: "Http://localhost:7601/api/values/GetData?"). Name= Liu Yifei ",                    success:function (r) {                        alert (r);                    }                );            });

Get the results you want.

Webapi-Chuan-shen (I.)

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.