How do I operate the ASP. NET Web API?

Source: Internet
Author: User
after three articles of my doubts, Webapi I believe that everyone has no problem!

First, create a Usermodel

public class Usermodel{public string UserID {get; set;} public string UserName {get; set;}}

Then add the Web API Controller

public class Usercontroller:apicontroller{public Usermodel getadmin ()    {return new Usermodel () {UserID = "$", User Name = "Admin"};    } }

Registering routes

public static void Register (Httpconfiguration config) {    config. Routes.maphttproute (        name: "Defaultapi",        routetemplate: "Api/{controller}/{id}",        defaults:new {id = Routeparameter.optional}    );}

Register in Global

protected void Application_Start (object sender, EventArgs e) {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 ({    type: ' GET ',    URL: ' api/user/getadmin ',    dataType: ' json ',    success:function (data, Textstatus) {        alert (data. UserID + "|" + data. UserName);    },    error:function (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

public bool Add (Usermodel user) {return 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:

<input type= "button" Name= "Btnok" id= "Btnok" value= "send Post request"/>

Add JS Code

$ (' #btnOK '). Bind (' click ', function () {//Create Ajax request, send data to background processing var postdata = {        UserID: ' 001 ',        UserName: ' Qeefee ' };    $.ajax ({        type: ' POST ',        URL: ' Api/user/add ',        data:postdata,        dataType: ' json ',        success: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!

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.