asp.net web api实例

1.Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication5.Models
{
    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;
        }
    }
}
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 () {

                $.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);

                })

            })
            $("#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>
</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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值