当前位置:Gxlcms > 数据库问题 > MVC跨域API HTTPClient方法 EF DBfirst

MVC跨域API HTTPClient方法 EF DBfirst

时间:2021-07-01 10:21:17 帮助过:24人阅读

-----------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   

人气教程排行