Today, referring to the official Microsoft (http://www.asp.net) study Webapi, here excerpt from the following:
Objective
HTTP is not just for generating web pages. It is also a powerful platform that can build APIs for exposing services and data. HTTP is simple and flexible, and it seems to be ubiquitous. Almost any platform you can think of can have an HTTP library, so HTTP services can be applied to a wide range of clients, such as browsers, mobile devices, and traditional desktop applications.
The ASP. NET Web API is a framework for building Web APIs on the. In this tutorial, you will use the ASP. NET Web API to create a list of products returned by the Web APIs.
Create a project
Create a Web Empty template project, select the Webapi core file,
Add a model
Under the Models folder, create a Product.cs:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
namespace ApiDemo01.Models {
/// <summary>产品实体类</summary>
public class Product
{
/// <summary>产品ID</summary>
public int ID {
get
;
set
; }
/// <summary>产品名称</summary>
public string Name {
get
;
set
; }
/// <summary>产品类别</summary>
public string Category {
get
;
set
; }
/// <summary>产品价格</summary>
public decimal Price {
get
;
set
; }
} }
|
Note: For the VS bracket to identify the entity class, remember the project as Mr.
Add Controller
Under the Controllers folder, use the bracket:
The bracket automatically generates some operation codes, which are modified here as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using ApiDemo01.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http;
namespace ApiDemo01.Controllers {
public class ProductController : ApiController
{
//模拟数据
List<Product> pList =
new List<Product>
{
new Product{ID=1, Name=
"Dell"
, Category=
"电脑" , Price=3500 },
new Product{ID=2, Name=
"Apple"
, Category=
"手机" , Price=5500 },
new Product{ID=3, Name=
"HP"
, Category=
"电脑" , Price=3000 }
};
//获取产品集合
public IEnumerable<Product> GetProducts()
{
return pList;
}
//根据产品ID获取一个产品
public IHttpActionResult GetProduct(
int id)
{
var product = pList.FirstOrDefault((p) => p.ID == id);
if (product ==
null
)
{
return NotFound();
}
return Ok(product);
}
} }
|
Note: There is no way to read the database, use the collection initializer.
Install jquery
To use the AJAX request, install the jquery library first:
Note: You can also copy the downloaded jquery.
Create a Presentation page
Add a index.html page under the project root directory, with the following code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<!DOCTYPE html>
<meta charset=
"utf-8" />
<title>产品页</title> <body>
<div>
<ul id=
"products" />
</div>
<div>
<input type=
"text" id=
"prodId" />
<input type=
"button" value=
"Search" onclick=
"find();" />
<p id=
"product" />
</div>
<script src=
"Scripts/jquery-2.1.0.js"
></script>
<script>
var uri =
‘api/product‘
;
$(document).ready(function () {
$.getJSON(uri)
.done(function (data) {
// 请求成功
$.each(data, function (key, item) {
$(
‘<li>‘
, { text: formatItem(item) }).appendTo($(
‘#products‘
));
});
});
});
function formatItem(item) {
return item.Name +
‘: $‘ + item.Price;
}
function find() {
var id = $(
‘#prodId‘
).val();
$.getJSON(uri +
‘/‘ + id)
.done(function (data) {
$(
‘#product‘
).text(formatItem(data));
})
//请求失败
.fail(function (jqXHR, textStatus, err) {
$(
‘#product‘
).text(
‘Error: ‘ + err);
});
}
</script> </body>
|
Note: jquery writes Ajax methods in a variety of ways!
Preview
Browse the Index.html page and enter find to get the following results:
Using the IE Developer tool (press F12), look at the request header:
Note: Webapi The default pass data serializes the JSON format, and the client does not have to write the deserialization code. (As for how it is done, it is explained later.) )