zoukankan      html  css  js  c++  java
  • C#Apit

    首先呢,同志们,我们需要  引用两个插件,

    之后创建两个项目。

    之后创建API服务端。和  Ef链接数据库  

    之后写好API

    如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.Cors;
    using WebApi_ZK3_.Models;

    namespace WebApi_ZK3_.Controllers
    {
    [EnableCors("*", "*", "*")]
    public class UserInfoController : ApiController
    {
    // GET: api/UserInfo
    ZK3Entities db = new ZK3Entities();
    /// <summary>
    /// 显示
    /// </summary>
    /// <returns></returns>
    public IEnumerable<UserInfo> Get()
    {
    return db.UserInfoes.ToList();
    }

    // GET: api/UserInfo/5
    /// <summary>
    /// 单条数据查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public IEnumerable<UserInfo> Get(int id)
    {
    var us = db.UserInfoes.Where(T => T.I_ID == id);
    return us;
    }
    [HttpPost]
    // POST: api/UserInfo
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="u"></param>
    public void Post([FromBody]UserInfo u)
    {
    db.UserInfoes.Add(u);
    db.SaveChanges();

    }
    // PUT: api/UserInfo/5
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="u"></param>
    public void Put(int id, [FromBody]UserInfo u)
    {
    var us = db.UserInfoes.Where(T => T.I_ID == id).FirstOrDefault();
    if (us != null)
    {
    us.I_Num = u.I_Num;
    us.I_Type = u.I_Type;
    us.I_Money = u.I_Money;
    us.I_Name = u.I_Name;
    us.I_Datetime = u.I_Datetime;
    db.SaveChanges();
    }

    }

    // DELETE: api/UserInfo/5
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public HttpResponseMessage Delete(int id)
    {
    var us = db.UserInfoes.Where(T => T.I_ID == id).FirstOrDefault();
    if (us != null)
    {
    db.UserInfoes.Remove(us);
    db.SaveChanges();
    return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK };
    }
    else
    {
    return new HttpResponseMessage() { StatusCode=HttpStatusCode.NoContent};
    }
    }
    }
    }

    上面是API   

    到这一步  API已经完成  

    请打开MVC项目

    创建控制器,编写以下代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Newtonsoft.Json;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using MVC_ZK3_.Models;
    using Webdiyer.WebControls.Mvc;//分页

    namespace MVC_ZK3_.Controllers
    {
    public class UserInfoController : Controller
    {
    // GET: UserInfo
    /// <summary>
    /// 显示+模糊+分页
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public ActionResult Index(string name = "" , int pageIndex = 1)
    {

    Uri url = new Uri("http://localhost:53076/");
    HttpClient client = new HttpClient();
    client.BaseAddress = url;
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage mess = client.GetAsync("/api/UserInfo").Result;
    if (mess.IsSuccessStatusCode)
    {
    var list = mess.Content.ReadAsStringAsync().Result;
    List<InfoM> m = JsonConvert.DeserializeObject<List<InfoM>>(list);

    if (name == "")
    {
    return View(m /*.ToPagedList(pageIndex, 2)*/);
    }
    else
    {
    List<InfoM> fm = (m.Where(T => T.I_Name.Contains(name)).ToList());
    return View(fm /*.ToPagedList(pageIndex,2)*/);
    }
    }
    return View("");
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public ActionResult Delete(int id)
    {
    Uri url = new Uri("http://localhost:53076/");
    HttpClient client = new HttpClient();
    client.BaseAddress = url;
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //返回
    HttpResponseMessage mess = client.DeleteAsync("api/UserInfo/" + id).Result;
    //判断
    if (mess.IsSuccessStatusCode)
    {
    return Content("<script>alert('删除成功');location.href='/UserInfo/Index'</script>");
    }
    return Content("<script>alert('删除失败')</script>");

    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <returns></returns>
    public ActionResult Post()
    {
    return View();
    }
    [HttpPost]
    public ActionResult Post(InfoM m)
    {
    Uri url = new Uri("http://localhost:53076/");
    HttpClient client = new HttpClient();
    client.BaseAddress = url;
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //定义对象
    HttpContent cen = new StringContent("{'I_Num':'" + m.I_Num + "','I_Type':'" + m.I_Type + "','I_Money':'" + m.I_Money + "','I_Name':'" + m.I_Name + "','I_Datetime':'" + m.I_Datetime + "'}");
    cen.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
    HttpResponseMessage mess = client.PostAsync("/api/UserInfo",cen).Result;
    if (mess.IsSuccessStatusCode)
    {
    return Content("<script>alert('添加成功');location.href='/UserInfo/Index'</script>");;
    }
    client.Dispose();
    return Content("<script>alert('添加失败')</script>");
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <returns></returns>
    public ActionResult Put(int id)
    {
    Uri url = new Uri("http://localhost:53076/");
    HttpClient client = new HttpClient();
    client.BaseAddress = url;
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage mess = client.GetAsync("/api/UserInfo/" + id).Result;
    if (mess.IsSuccessStatusCode)
    {
    var list = mess.Content.ReadAsStringAsync().Result;
    var m = JsonConvert.DeserializeObject<List<InfoM>>(list);

    InfoM i = new InfoM();
    i.I_ID = m[0].I_ID;
    i.I_Money = m[0].I_Money;
    i.I_Name = m[0].I_Name;
    i.I_Num = m[0].I_Num;
    i.I_Type = m[0].I_Type;
    i.I_Datetime = m[0].I_Datetime;
    return View(i);
    }

    return View("");
    }
    [HttpPost]
    public ActionResult Put(InfoM m)
    {
    Uri url = new Uri("http://localhost:53076/");
    HttpClient client = new HttpClient();
    client.BaseAddress = url;
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //定义对象
    HttpContent cen = new StringContent("{'I_ID':'" + m.I_ID + "','I_Num':'" + m.I_Num + "','I_Type':'" + m.I_Type + "','I_Money':'" + m.I_Money + "','I_Name':'" + m.I_Name + "','I_Datetime':'" + m.I_Datetime + "'}");
    cen.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
    HttpResponseMessage mess = client.PutAsync("/api/UserInfo/" + m.I_ID, cen).Result;
    if (mess.IsSuccessStatusCode)
    {
    return Content("<script>alert('修改成功');location.href='/UserInfo/Index'</script>");
    }
    client.Dispose();
    return Content("<script>alert('修改失败');location.href='/UserInfo/Index'</script>");
    }

    }
    }

    切记哦,MVC里面需要写个类  进行数据交互

    显示代码如下


    @{
    Layout = null;
    }
    @using Webdiyer.WebControls.Mvc;
    @model PagedList<MVC_ZK3_.Models.InfoM>
    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <style>
    table {
    border:1px solid #ff0000;
    900px;
    text-align:center;
    }
    table tr {
    border:1px solid #ff0000;
    }
    table tr td {
    border:1px solid #ff0000;
    }
    </style>
    </head>
    <body>
    <div>

    <a href="Post">添加</a>&nbsp;
    <input id="Text1" type="text" placeholder="请输入缴费人姓名" />
    <input id="Button1" type="button" value="查询" onclick="cha()" />
    <table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
    <td>编号</td>
    <td>门牌号</td>
    <td>缴费类型</td>
    <td>缴费金额</td>
    <td>缴费人</td>
    <td>缴费时间</td>
    <td>操作</td>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
    <tr>
    <td>@item.I_ID</td>
    <td>@item.I_Num </td>
    <td>@item.I_Type </td>
    <td>@item.I_Money </td>
    <td>@item.I_Name </td>
    <td>@item.I_Datetime</td>
    <td>
    <a href="#" onclick="del(@item.I_ID)">删除</a>
    <a href="#" onclick="updt(@item.I_ID)">修改</a>
    </td>
    </tr>
    }
    </tbody>
    </table>
    //分页
    @Html.Pager(Model,new PagerOptions() { PageIndexParameterName= "pageIndex" })
    </div>
    <script>
    //删除
    function del(id)
    {
    location.href = "/UserInfo/Delete?id="+id;
    }
    //修改
    function updt(id)
    {
    location.href="/UserInfo/Put?id="+id;

    }
    //查询
    function cha()
    {
    var name = $("#Text1").val();
    location.href = "/UserInfo/Index?name="+name;
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    Python13_安装、解释器
    Python12_关于文件概念的讨论与序列化
    Python11_文件的读写
    which | whereis |locate |find
    tail命令 | head命令
    cat 命令|more命令|less命令
    数据库模型设计,第一范式、第二范式、第三范式简单例子理解
    json
    正则表达式
    SFTP相关命令
  • 原文地址:https://www.cnblogs.com/dxylx/p/7880937.html
Copyright © 2011-2022 走看看