MVC cross-Domain API HttpClient method EF Dbfirst

Source: Internet
Author: User

-----------API section//Hook up API

public class Productcontroller:apicontroller
{

Aentities db = new aentities ();//reference db

Public ienumerable<zuohebiao> Get (string name, int id)
{
list<zuohebiao> list = new list<zuohebiao> ();//Instantiate a combination class to receive data

var AA = db. Xitong.tolist ();//Information table
var BB = db. Typeinfo.tolist ();//Type table
List = (from a in AA//Using a LINQ connection query
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))//judgment
{
list= list. Where (P = = p.getmoneyman.contains (name)). ToList ();//Replace List

}
if (ID > 0)
{
List = list. Where (p = P.typeid = = ID). ToList ();
}
return list;

}
[HttpGet]
Public Xitong getone (int id)//query single data
{
Return DB. Xitong.where (T = = T.id = = ID). FirstOrDefault ();
}
[HttpGet]
Public UserInfo Login (string name, string pwd)//Login
{
Return DB. Userinfo.where (T = t.name==name&& t.name==pwd). FirstOrDefault ();
}



public void Post ([Frombody]xitong value)//New data
{
Db. Xitong.add (value);
Db. SaveChanges ();
}


public void Put ([Frombody]xitong value)//Modify
{
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)//delete
{
var data = db. Xitong.where (T = = T.id = = ID). FirstOrDefault ();
Db. Xitong.remove (data);
Db. SaveChanges ();
}
}

-----------------------------MVC Section

public class Showcontroller:controller
{
Aentities db = new aentities ();
public static readonly URI address = new Uri ("http://localhost:9556");
Login
[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 (' Login succeeded! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Login failed! ') </script> ");
}
}
}
Show
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));
}
Delete
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 (' Delete succeeded! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Delete failed! '); location.href= '/show/index ' </script> ");
}
}
}
Add to
[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 (' Add success! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Add failed! '); location.href= '/show/index ' </script> ");
}
}
}
Modify
[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" modified successfully! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Modify failed! '); location.href= '/show/index ' </script> ");
}
}
}
}

--------------Display

------------modification

Route plus action

MVC cross-Domain API HttpClient method EF Dbfirst

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.