ASP. NET Web Api owinselfhost Restful use

Source: Internet
Author: User

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


      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

Related Article

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: 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.