An initial exploration of ASP Webapi (i.)

Source: Internet
Author: User

I do not have in-depth research on WEBAPI, the initial contact found in the data request a few phenomena.

First cut into the code

1. If the action starts with GET, the default is Get mode, without get the default is the Post method

  Public stringGetUsers0 (stringID) {List<UserProfile> list = Userprofilebiz.getbatchtest () asList<userprofile>; //returns a JSON string            returnjsonconvert.serializeobject (list); } $ (function () {$.ajax ({URL:"Http://localhost:6556/api/User/GetUsers0", type:"Get", DataType:"JSON", data: {"ID":"1"}, Beforesend:function (XMLHttpRequest) {}, Success:function (data, status) { $ (data). each (function () {alert ( This.                UserName);            });    }, Complete:function (data, status) {}, Error:function () {}}); });

The Get method can request to the value, the ID case problem can be ignored, but if changed to post, of course, the request is not

If the request is changed to post

will have to

[HttpPost]          Public string GetUsers0 ([frombody]string  id)        {            Listas list<userprofile>;              // returns a JSON string            return jsonconvert.serializeobject (list);                   }

Now say action to any without get start, and then use get to request Discovery request, this is because the action by default has become the way of POST request, after changing to post, no problem

 Public stringMyUsers0 ([Frombody]stringID) {List<UserProfile> list = Userprofilebiz.getbatchtest () asList<userprofile>; //returns a JSON string            returnjsonconvert.serializeobject (list); } $.ajax ({URL:"Http://localhost:6556/api/User/MyUsers0", type:"Post", DataType:"JSON", data: {"ID":"1"}, Beforesend:function (XMLHttpRequest) {}, Success:function (data, status) { $ (data). each (function () {alert ( This.                UserName);            }); }, Complete:function (data, status) {}, Error:function () {}}); 

But still need a frombody attribute declaration, for non-object type of the value of the way

What happens if I change to an entity parameter?

 Public string MyUsers0 (userprofile user)        {            Listas list<userprofile>;              // returns a JSON string            return jsonconvert.serializeobject (list);                   }

You do not need to declare the Frombody attribute for an entity type parameter.

As to why we need to continue to explore.

An initial exploration of ASP Webapi (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.