1. 程式人生 > >MVC跨域API HTTPClient方法 EF DBfirst

MVC跨域API HTTPClient方法 EF DBfirst

rom 類型 修改 cti rod page 新增 rabl public

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