1.
2.
3.
上面三步完成 weiApi 項目創建
運行項目
新建一個Products API
(1)、添加ProductController
(2)、選擇empty 控制器
前後臺代碼
html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
$(function () {
$("#GetProductList").click(function () {
$.ajax({
type: "get",
url: "/api/products/product/GetProductList",
success: function (data, status) {
}
});
});
$("#GetProduct").click(function () {
$.ajax({
type: "get",
url: "/api/products/product/get?productId=111",
success: function (data, status) {
}
});
});
$("#AddProduct").click(function () {
$.ajax({
type: "post",
url: "/api/products/product/add",
contentType: 'application/json',
data: JSON.stringify({ Title: "腦白金", CreateTime: "1988-09-11" }),
success: function (data, status) { }
});
});
$("#UpdateProduct").click(function () {
$.ajax({
type: "post",
url: "/api/products/product/update?productId=111",
contentType: 'application/json',
data: JSON.stringify({ Title: "腦白金升級版", CreateTime: "1988-09-11" }),
success: function (data, status) { }
});
});
$("#DeleteProduct").click(function () {
$.ajax({
type: "delete",
url: "/api/products/product/delete?productId=111",
contentType: 'application/json',
success: function (data, status) { }
});
});
});
</script>
</head>
<body>
<input type="button" id="GetProductList" value="獲取產品分頁API" /><br />
<input type="button" id="GetProduct" value="獲取單個產品API" /><br />
<input type="button" id="AddProduct" value="產品新增API" /><br />
<input type="button" id="UpdateProduct" value="產品更新API" /><br />
<input type="button" id="DeleteProduct" value="產品刪除API" /><br />
</body>
</html>
重點內容 後端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApplication1.Controllers
{
[RoutePrefix("api/products")]
public class ProductController : ApiController
{
//獲取產品分頁API: api/products/product/getList
[HttpGet, Route("product/getList")]
public List<Product> GetProductList()
{
throw new NotImplementedException();
}
//獲取單個產品API: api/products/product/get?productId=產品ID
[HttpGet, Route("product/get")]
public Product GetProduct(int productId)
{
throw new NotImplementedException();
}
//產品新增API: api/products/product/add
[HttpPost, Route("product/add")]
public Guid AddProduct(Product product)
{
throw new NotImplementedException();
}
//產品更新API: api/products/product/update?productId=產品ID
[HttpPost, Route("product/update")]
public void UpdateProduct(int productId, Product product)
{
throw new NotImplementedException();
}
//產品刪除API: api/products/product/delete?productId=產品ID
[HttpDelete, Route("product/delete")]
public void DeleteProduct(int productId)
{
throw new NotImplementedException();
}
}
public class Product
{
public int productId { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
}