码迷,mamicode.com
首页 > 数据库 > 详细

MVC跨域API HTTPClient方法 EF DBfirst

时间:2017-11-21 20:49:48      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:host   api   组合   dbf   turn   isnull   def   lin   cat   

-----------API 部分//勾上Api

  public class ProductController : ApiController
    {

        AEntities db = new AEntities();//引用db

        public IEnumerable<ZuoHeBiao> Get(string name, int id)
        {
            List<ZuoHeBiao> list = new List<ZuoHeBiao>();//实例化一个组合类用来接收数据

            var aa = db.XiTong.ToList();//信息表
            var bb = db.TypeInfo.ToList();//类型表
            list = (from a in aa                 //使用linq连接查询
                    join b in bb
                    on a.TypeID equals b.ID
                    select new ZuoHeBiao
                    {
                        ID = a.ID,
                        Door = a.Door,
                        TypeName = b.TypeName,
                        MoneyInfo = a.MoneyInfo,
                        GetMoneyman = a.GetMoneyman,
                        TimeInof = a.TimeInof,
                        TypeID=a.TypeID
                    }
                       ).ToList();
            if (!string.IsNullOrEmpty(name))//判定
            {
                list= list.Where(p => p.GetMoneyman.Contains(name)).ToList();//替换list
               
            }
            if (id > 0)
            {
                list = list.Where(p => p.TypeID == id).ToList();            
            }
            return list;

        }
        [HttpGet]
        public XiTong Getone(int id)//查询单条数据
        {
            return db.XiTong.Where(T => T.ID == id).FirstOrDefault();
        }
        [HttpGet]
        public UserInfo Login(string name, string pwd)//登录
        {
            return db.UserInfo.Where(T => T.Name==name&& T.Name==pwd).FirstOrDefault();
        }



        public void Post([FromBody]XiTong value)//新增数据
        {
            db.XiTong.Add(value);
            db.SaveChanges();
        }


        public void Put([FromBody]XiTong value)//修改
        {
            var data = db.XiTong.Where(T => T.ID == value.ID).FirstOrDefault();
            if (data != null)
            {
                data.Door = value.Door;
                data.TypeID = value.TypeID;
                data.MoneyInfo = value.MoneyInfo;
                data.GetMoneyman = value.GetMoneyman;
                data.TimeInof = value.TimeInof;
            }
            db.SaveChanges();
        }


        public void Delete(int id)//删除
        {
            var data = db.XiTong.Where(T => T.ID == id).FirstOrDefault();
            db.XiTong.Remove(data);
            db.SaveChanges();
        }
    }

-----------------------------MVC 部分

 public class ShowController : Controller
    {
        AEntities db = new AEntities();
        public static readonly Uri address = new Uri("http://localhost:9556");
        //登录
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult LoginInfo(string name,string pwd)
        {
            Uri url = new Uri(address, "/apiD/product/Login?name=" + name + "&pwd=" + pwd);
            using (HttpClient client = new HttpClient())
            {
                var result = client.GetAsync(url).Result;
                if(result.IsSuccessStatusCode)
                {
                    return Content("<script>alert(‘登录成功!‘);location.href=‘/Show/Index‘</script>");
                }
                else
                {
                    return Content("<script>alert(‘登录失败!‘)</script>");
                }
            }
        }
        // 显示
        public ActionResult Index(string name = "", int id = 0, int pageIndex = 1)
        {
            var select = db.TypeInfo;
            ViewBag.list = new SelectList(select, "ID", "TypeName");
            Uri url = new Uri(address, "/apiD/product/Get?name=" + name+"&id="+id);
            List<ZuoHeBiao> tt = new List<ZuoHeBiao>();
            using (HttpClient client = new HttpClient())
            {
                var result = client.GetAsync(url).Result;
                if(result.IsSuccessStatusCode)
                {
                    tt = result.Content.ReadAsAsync<List<ZuoHeBiao>>().Result;
                }
            }
                return View(tt.ToPagedList(pageIndex,2));
        }
        //删除
        public ActionResult Delete(int id)
        {
            Uri url = new Uri(address, "/apiD/Product/Delete?id="+id);
            using (HttpClient client = new HttpClient())
            {
                var result = client.DeleteAsync(url).Result;
                if (result.IsSuccessStatusCode)
                {
                    return Content("<script>alert(‘删除成功!‘);location.href=‘/Show/Index‘</script>");
                }
                else
                {
                    return Content("<script>alert(‘删除失败!‘);location.href=‘/Show/Index‘</script>");
                }
            }      
        }
        //添加
        [HttpGet]
        public ActionResult Add()
        {
            var select = db.TypeInfo;
            ViewBag.list = new SelectList(select, "ID", "TypeName");
            return View();
        }
        public ActionResult Add(XiTong str)
        {
            
            Uri url = new Uri(address, "/api/Product");
            using (HttpClient client = new HttpClient())
            {
                var result = client.PostAsJsonAsync(url,str).Result;
                if (result.IsSuccessStatusCode)
                {
                    return Content("<script>alert(‘添加成功!‘);location.href=‘/Show/Index‘</script>");
                }
                else
                {
                    return Content("<script>alert(‘添加失败!‘);location.href=‘/Show/Index‘</script>");
                }
            }
        }
        //修改
        [HttpGet]
        public ActionResult Put()
        {
            var select = db.TypeInfo;
            ViewBag.list = new SelectList(select, "ID", "TypeName");
            return View();
        }
        public ActionResult Put(XiTong str)
        {

            Uri url = new Uri(address, "/apiD/product/Put");
            using (HttpClient client = new HttpClient())
            {
                var result = client.PutAsJsonAsync(url, str).Result;
                if (result.IsSuccessStatusCode)
                {
                    return Content("<script>alert(‘修改成功!‘);location.href=‘/Show/Index‘</script>");
                }
                else
                {
                    return Content("<script>alert(‘修改失败!‘);location.href=‘/Show/Index‘</script>");
                }
            }
        }
    }

--------------显示

技术分享图片

------------修改

技术分享图片

路由加上Action

 

MVC跨域API HTTPClient方法 EF DBfirst

标签:host   api   组合   dbf   turn   isnull   def   lin   cat   

原文地址:http://www.cnblogs.com/NETSBW/p/7875080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!