webapi Introduction and parameter binding

Description:
WebAPI technology used to develop interfaces between systems, based on the HTTP protocol, the default is to return json format. Wcf simpler than the more general, more lightweight, more provincial traffic (json format);
WebAPI as multiplexing MVC routing, ModelBinder, Filter and other knowledge, but merely imitating
webapi default routing mechanism is through http request type matches Action (REST style ), and the default routing mechanism MVC is matched by the url Action.
Webapi can modify the default routing mechanism, by matching action url

Restfull style:
that is to design communication protocol based on a predicate semantics (use get, post, put, delete request interface, and returns the result via http status code, but too difficult to use)

webapi binding parameters:
GET Request:
physical parameters, parameters need to add solid [FromUri]

post request:
single parameter, and needs [FromBody], key need to change the empty string, WTF?
Physical parameters, no need to add [FromBody]
physical parameters can also be used JObject parameter receiving
can be transferred json format string, automatically binding entities (request type should be set to json format contentType: json)

 

    public class ValuesController : ApiController
    {
        [HttpGet]
        public string Index([FromUri]User user)
        {
            return "index1";
        }
        [HttpGet]
        public string Index33(string name, int id)
        {
            return "index33";
        }
        [HttpGet]
        public string Index2(int id)
        {
            return "index2";
        }
        [HttpPost]
        public string PIndex1(int id)
        {
            return "pindex1";
        }
        [HttpPost]
        public string PIndex2([FromBody]int id)
        {
            return "pindex2";
        }
        [HttpPost]
        public string PIndex3(User user)
        {
            return "pindex3";
        }
        [HttpPost]
        public string PIndex5(JObject jobj)
        {
           var user1 = jobj["UserInfo"].ToObject<User>();
            var dog1 = jobj["DogInfo"].ToObject<Dog>();
            return "pindex5";
        }[HttpPost]
        public  string PIndex6 (jobject jobj)
        {
           var usider1 = jobj["id"].ToObject<int>();
            var name = jobj["name"].ToObject<string>();
            return "pindex6";
        }
    }
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class Dog
    {
        public string DogName { get; set; }
    }

Guess you like

Origin www.cnblogs.com/fanfan-90/p/12041806.html