After three articles of my doubts, Webapi I believe that everyone has no problem!
First, create a Usermodel
Usermodel{ getsetgetset;}}
Then add the Web API Controller
Apicontroller{ usermodel "};}}
Registering routes
Register (config) { config. Routes.maphttproute ( "Defaultapi" "Api/{controller}/{id}"Routeparameter. Optional});}
Register in Global
Application_Start (webapiconfig. Register (globalconfiguration. Configuration);}
This time use the address bar to access the address: api/user/getadmin
At this time, the XML data model is returned by default.
Using AJAX to request this API, specify the data format as JSON
$.ajax ({ ' GET ', ' api/user/getadmin ', ' json '(XMLHttpRequest, Textstatus, Errorthrown) {}});
The result of alert is:
So it's really dudu that you can return the specified data format based on the requested data type.
Post data
Modify the controller and add a method
Add (user) { null;}
Just for testing, so here's just a look at whether the incoming entity is empty or true if it's not empty
I added a button to the page with the following code:
<typenameidValue= "send Post request"/>
Add JS Code
$(' #btnOK '). Bind (' Click 'function () {//create Ajax request to send data to background processing var postdata = {UserID: ' 001 ' ' Qeefee ' ' POST ' , URL: " Api/user/add ' ' json ' function (data, textstatus) {alert ( data); }, Error: function (XMLHttpRequest, Textstatus, ErrorThrown) {} });});
Run the page again
We attach the process to debug, the data received by the server segment when the AJAX request is sent
If you think this article is helpful, don't forget to support it!
Fei Qi Source: http://youring2.cnblogs.com/
Explore how to manipulate ASP. NET Web API (iii)