Home > Backend Development > C#.Net Tutorial > How to operate ASP.NET Web API?

How to operate ASP.NET Web API?

零下一度
Release: 2017-06-26 15:28:17
Original
1473 people have browsed it

After clearing up the confusion in my three articles, I believe everyone has no problem with webapi!

First create a UserModel

public class UserModel{public string UserID { get; set; }public string UserName { get; set; }
}
Copy after login

Then add Web API Controller

public class UserController : ApiController{public UserModel getAdmin()
    {return new UserModel() { UserID = "000", UserName = "Admin" };
    } 
}
Copy after login

Register Route

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}
Copy after login

Register in Global

protected void Application_Start(object sender, EventArgs e)
{WebApiConfig.Register(GlobalConfiguration.Configuration);
}
Copy after login

At this time, use the address bar to access the address: api/user/getadmin

How to operate ASP.NET Web API?

At this time, the XML data model is returned by default .

Use 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) {
    }
});
Copy after login

The result of alert is:

How to operate ASP.NET Web API?

It seems like this, really What dudu said is that the specified data format can be returned according to the requested data type.

POST data

Modify the controller and add an add method

public bool add(UserModel user)
{return user != null;
}
Copy after login

Just for testing, so here we only judge whether the incoming entity is Empty, if not empty it returns true

I added a button on the page, the code is as follows:

<input type="button" name="btnOK" id="btnOK" value="发送POST请求" />
Copy after login

Add JS code

$(&#39;#btnOK&#39;).bind(&#39;click&#39;, function () {//创建ajax请求,将数据发送到后台处理var postData = {
        UserID: &#39;001&#39;,
        UserName: &#39;QeeFee&#39;};
    $.ajax({
        type: &#39;POST&#39;,
        url: &#39;api/user/add&#39;,
        data: postData,
        dataType: &#39;json&#39;,
        success: function (data, textStatus) {
            alert(data);
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
        }
    });
});
Copy after login

Run the page again

How to operate ASP.NET Web API?

We attach the process for debugging. When sending an ajax request, the data received by the server segment is as shown in the figure:

How to operate ASP.NET Web API?

If you think this article is helpful to you, don’t forget to support it!

The above is the detailed content of How to operate ASP.NET Web API?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template