Parameter passing cases from simple types to complex types, and solutions for passing simple string types

Source: Internet
Author: User

One, the simple type of the pass value

  such as public Users get (int id), which can be obtained in two ways:  api/default/5$.get ("/api/default", {id:90}, function (data) {/* Processing logic */}), the former does not need to specify the parameter name, the latter is applicable to the existence of a number of simple parameters, such as a more practical case and the corresponding acquisition method is:  public Users get (int id, int id2) $.get ("/api/default ", {id:90,id2:88}, function (data) {}), second, the pass-through of a string is involved in a simple type pass   for a simple type parameter, the only thing that can be called a problem is that it is encountered for example: public string Post (string v) Such a case if you post a string with a parameter named V past, for example: $.post ("/api/teststring", {V: "I Want TestString"}, function (data) {}); , then the result is reactive:     by searching for StackOverflow and Encosia (see here), the solution:  first overrides the [Frombody] feature for the parameter, such as public string Post ([frombody]string v), then:  solution 1:$.post ("/api/teststring", "=i want TestString", function (data) { }); Precede with an equals sign solution 2:$.post ("/api/teststring", {"" ":" I Want TestString "}, function (data) {}); Passing an empty parameter name problem is solved, but I also nag a sentence: this is like what words.   Admittedly some friends will say "Web API is not used, it is for a certain situation ... You should construct an object ... "but, since there is such a usage, the solution to the problem that this article is addressing is the one that may arise.     three, passing a complex type:  first defines two types,     public class users    {        public int uid {get; set;}          public string Username {get; set;}    }    public class doublestringprameter    {        public string Pra M1 {get; set;}          public string Pram2 {get; set;}     for cases where you need to send two string arguments, you must pass an object:  public string Post (Doublestringprameter pram) $.post ("/api/ Teststringusingobject ", {Pram1:" Value of Parameter 1 ", Pram2:" Value of Parameter 2 "}, function (data) {}); You do not need to specify a parameter name and are required to pass more complex objects, such as passing both Doublestringprameter and Users, so encapsulate:        public Class using2objcontroller:apicontroller    {         public string Post (Imultiobj obj )         {            return "UID:" + obj. User.uid + ", Username:" + obj. User.username + "| | PRAM1: +                 obj. Stringprameter.pram1 + ", pram2:" + obj. stringprameter.pram2;       }    }     public class Imultiobj//define a class Type package     {        public doublestringprameter stringprameter {get; set;}          public Users User {get; set;}    }    then pass:       $.post ("/api/using2obj", {User: {uid: ' 80909 ', Usern Ame: ' Amazon ', Stringprameter: {Pram1: ' Value of parameter 1 ', PRAM2: ' Parameter 2 ' value '}},  function (data) {});       For simple type pass-through of string, my opinion is: as an API, if you provide some functionality, then you have to implement, if you do not or do not want to do, it should be during compilation to break the possibility of the problem occurs (you should not let the Post (string a), Post (string A, string B), post (Users  u1, users U2) are compiled, rather than taking a "dialect"-level restriction on the user during use, which is already incompatible with the design of a strongly typed language. Think of the situation: a certain night, a work nap wrote the post (Users  user, content content), compiled, one months after the client has done 20,000 lines of code, and then said that can not be used (do not use what you also write to do), This is a design malpractice.   These are not problem issues that still exist on 2.0, which is both a bug and not a bug.   is more inclined to use WCF or MVC's return Json () for the Web API's problem itself.

Parameter passing cases from simple types to complex types, and solutions for passing simple string types

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.