First, preface
Summarize what a restful architecture is:
(1) Each URI represents a resource;
(2) between the client and the server, the transmission of such resources of some kind of performance layer;
(3) The client through four HTTP verbs, the server-side resources to operate, to achieve "performance layer State transformation."
Ii. examples
The Restful route template does not have {action} by default, and you need to enable the property routing feature for use with special needs
1.startup.cs
Public voidConfiguration (Iappbuilder app) {httpconfiguration config=Newhttpconfiguration (); //Enable Web API feature routingCONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); //To Delete an XML serializerCONFIG. Formatters.remove (config. Formatters.xmlformatter); //Configure cross-domainapp. Usecors (Corsoptions.allowall); App. Usewebapi (config); }
2. Code examples
1[Routeprefix ("Home")]2 Public classHomecontroller:apicontroller3 {4 //GET api/home5 [HttpGet]6 Public stringGet ()7 {8 return "Test";9 }Ten One [HttpGet] A //GET API/HOME/5 - Public stringGet (intID) - { the returnID. ToString (); - } - - [HttpGet] + //GET api/home?name=test - Public stringGet (stringname) + { A returnname; at } - - [HttpGet] - //GET api/home/1?name=test - //ID can match to {ID} route template - Public stringGet (stringNameintID) in { - returnName +ID; to } + - the //GET api/home/1?name=test&age=30 * [HttpGet] $ Public stringGet (intIdstringNameintAge )Panax Notoginseng { - returnID + name +Age ; the } + A //POST api/home the [HttpPost] + Public voidPost (person p) - { $ } $ - - //POST Home/post2 the [HttpPost] -[Route ("Post2")]Wuyi Public voidPost2 (person p) the { - } Wu - [Httpput] About //PUT API/HOME/5 $ Public voidPut (stringID, person p) - { - } - A //PUT Home/put2 + [Httpput] the[Route ("Put2")]//multiple post or put need to go action - Public voidPut2 (person p) $ { the } the the //DELETE API/HOME/5 the [Httpdelete] - Public voidDelete (intID) in { the } the About //DELETE api/home/5?type=1&name=test the [Httpdelete] the Public voidDelete (intIdstringTypestringname) the { + } -}
ASP. NET Web Api owinselfhost Restful use