Explore how to manipulate ASP. NET Web API (iii)

Source: Internet
Author: User

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)

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