1.Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication5.Models
{
public class Class1
{
public int ID { get; set; }
{
public class Class1
{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Birthday { get; set; }
public int Age { get; set; }
}
}
}
}
2.Controllers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication5.Models;
namespace WebApplication5.Controllers
{
public class DefaultController : ApiController
{
Class1[] contacts = new Class1[]
{
new Class1(){ID=1,Age=23,Birthday="1977-05-30",Name="情缘",Sex="男"},
new Class1(){ID=2,Age=55,Birthday="1937-05-30",Name="令狐冲",Sex="男"},
new Class1(){ID=3,Age=12,Birthday="1987-05-30",Name="郭靖",Sex="男"},
new Class1(){ID=4,Age=18,Birthday="1997-05-30",Name="黄蓉",Sex="女"},
};
/// <summary>
/// /api/Contact
/// </summary>
/// <returns></returns>
public IEnumerable<Class1> GetListAll()
{
return contacts;
}
public IEnumerable<Class1> SubMit([FromBody] Class1 c1)
{
Class1[] contacts1 = new Class1[] { c1 };
return contacts1;
}
/// <summary>
/// /api/Contact/id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public IEnumerable<Class1> GetContactByID(int id)
{
Class1 contact = contacts.FirstOrDefault<Class1>(item => item.ID == id);
if (contact == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
Class1[] contacts12 = new Class1[] { contact };
return contacts12;
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication5.Models;
namespace WebApplication5.Controllers
{
public class DefaultController : ApiController
{
Class1[] contacts = new Class1[]
{
new Class1(){ID=1,Age=23,Birthday="1977-05-30",Name="情缘",Sex="男"},
new Class1(){ID=2,Age=55,Birthday="1937-05-30",Name="令狐冲",Sex="男"},
new Class1(){ID=3,Age=12,Birthday="1987-05-30",Name="郭靖",Sex="男"},
new Class1(){ID=4,Age=18,Birthday="1997-05-30",Name="黄蓉",Sex="女"},
};
/// <summary>
/// /api/Contact
/// </summary>
/// <returns></returns>
public IEnumerable<Class1> GetListAll()
{
return contacts;
}
public IEnumerable<Class1> SubMit([FromBody] Class1 c1)
{
Class1[] contacts1 = new Class1[] { c1 };
return contacts1;
}
/// <summary>
/// /api/Contact/id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public IEnumerable<Class1> GetContactByID(int id)
{
Class1 contact = contacts.FirstOrDefault<Class1>(item => item.ID == id);
if (contact == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
Class1[] contacts12 = new Class1[] { contact };
return contacts12;
}
}
}
}
3.view html
<!DOCTYPE html>
<html>
<head>
<meta name ="viewport" content="width=device-width" />
<title>About</title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#btnAll").click(function () {
$.getJSON("/api/Default", function (data) {
var html = "<ul>";
$(data).each(function (i, item) {
html += "<li>" + item.ID + ":" + item.Name + ":" + item.Sex + "</li>";
});
html += "</ul>";
$("#contactAll").html(html);
});
});
$("#btnadd").click(function () {
<html>
<head>
<meta name ="viewport" content="width=device-width" />
<title>About</title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#btnAll").click(function () {
$.getJSON("/api/Default", function (data) {
var html = "<ul>";
$(data).each(function (i, item) {
html += "<li>" + item.ID + ":" + item.Name + ":" + item.Sex + "</li>";
});
html += "</ul>";
$("#contactAll").html(html);
});
});
$("#btnadd").click(function () {
$.post("/api/Default", { ID: 1, Name: "小米", Sex: "男", Birthday: "2015-01-02", Age: 30 }, function (data) {
var html = "<ul>";
$(data).each(function (i, item) {
html += "<li>" + item.ID + ":" + item.Name + ":" + item.Sex + "</li>";
});
html += "</ul>";
$("#contactAll").html(html);
$(data).each(function (i, item) {
html += "<li>" + item.ID + ":" + item.Name + ":" + item.Sex + "</li>";
});
html += "</ul>";
$("#contactAll").html(html);
})
})
$("#btnID").click(function () {
var id = $("#txtID").val();
alert(id);
$.getJSON("/api/Default/" + id, function (data) {
var html = "<ul>";
$(data).each(function (i, item) {
html += "<li>" + item.ID + ":" + item.Name + ":" + item.Sex + "</li>";
});
html += "</ul>";
$("#contactAll").html(html);
alert(html);
});
});
})
</script>
</script>
</head>
<body>
<p>
<input type="button" id="btnAll" value="查询所有" />
<input type="button" id="btnadd" value="添加数据" />
<input type="button" id="btnID" value="根据ID查询" />
<input type="text" id="txtID" />
</p>
<div id="contactAll"></div>
</body>
</html>
<body>
<p>
<input type="button" id="btnAll" value="查询所有" />
<input type="button" id="btnadd" value="添加数据" />
<input type="button" id="btnID" value="根据ID查询" />
<input type="text" id="txtID" />
</p>
<div id="contactAll"></div>
</body>
</html>