//页面跳转
<a href="/Home/Client_Delete/@item.BookID">删除</a>
<a href="/Home/Client_Put/@item.BookID">修改</a>
///WebApiConfig里修改路由配置
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
// 将 Web API 配置为仅使用不记名令牌身份验证。
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Actiontapi",
routeTemplate: "Actiontapi/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
//Api页面方法
day20_BookEntities db = new day20_BookEntities();
// GET: api/BookApi
public IEnumerable<OrderInfo> Get()
{
var data = from s in db.Book
join ss in db.TypeInfo on s.BookTid equals ss.Tid
select new OrderInfo()
{
BookID = s.BookID,
BookName = s.BookName,
BookTid = s.BookTid,
BookNum = s.BookNum,
BookPrice = s.BookPrice,
Tname = ss.Tname,
};
return data.ToList();
}
[HttpGet]
public List<OrderInfo> Select(int id)
{
var data = from s in db.Book
join ss in db.TypeInfo on s.BookTid equals ss.Tid
where s.BookTid == id
select new OrderInfo()
{
BookID = s.BookID,
BookName = s.BookName,
BookTid = s.BookTid,
BookNum = s.BookNum,
BookPrice = s.BookPrice,
Tname = ss.Tname,
};
return data.ToList();
}
// POST: api/BookApi
public void Post([FromBody]Book value)
{
db.Book.Add(value);
db.SaveChanges();
}
// PUT: api/BookApi/5
public void Put(int id, [FromBody]OrderInfo value)
{
var b = db.Book.Where(T => T.BookID == value.BookID).FirstOrDefault();
if (b != null)
{
b.BookID = value.BookID;
b.BookName = value.BookName;
b.BookTid = value.BookTid;
b.BookNum = value.BookNum;
b.BookPrice = value.BookPrice;
}
db.SaveChanges();
}
// DELETE: api/BookApi/5
public HttpResponseMessage Delete(int id)
{
var b = db.Book.Where(T => T.BookID == id).FirstOrDefault();
try
{
if (b != null)
{
db.Book.Remove(b);
db.SaveChanges();
return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK };
}
else
{
return new HttpResponseMessage() { StatusCode = HttpStatusCode.NoContent };
}
}
catch (Exception)
{
return new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError };
}
}
public ActionResult Client_Get()
{
//下拉列表
day20_BookEntities db = new day20_BookEntities();
var selectlist = db.TypeInfo;
SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
ViewBag.SelectList = list2;
Uri url = new Uri("http://localhost:52444");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.GetAsync("api/BookApi").Result;
List<OrderInfo> list = new List<OrderInfo>();
if (message.IsSuccessStatusCode)
{
list = message.Content.ReadAsAsync<List<OrderInfo>>().Result;
}
client.Dispose();
return View(list);
}
public ActionResult Client_Select(int id)
{
//下拉列表
day20_BookEntities db = new day20_BookEntities();
var selectlist = db.TypeInfo;
SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
ViewBag.SelectList = list2;
Uri url = new Uri("http://localhost:52444");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.GetAsync("Actiontapi/BookApi/Select/" + id).Result;
List<OrderInfo> list = new List<OrderInfo>();
if (message.IsSuccessStatusCode)
{
list = message.Content.ReadAsAsync<List<OrderInfo>>().Result;
}
client.Dispose();
return View("Client_Get", list);
}
public ActionResult Client_Post()
{
//下拉列表
day20_BookEntities db = new day20_BookEntities();
var selectlist = db.TypeInfo;
SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
ViewBag.SelectList = list2;
return View();
}
[HttpPost]
public ActionResult Client_Post(Book o)
{
Uri url = new Uri("http://localhost:52444");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent("{BookName:'" + o.BookName + "',BookTid:'" + o.BookTid + "',BookNum:'" + o.BookNum + "',BookPrice:'" + o.BookPrice + "'}");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage message = client.PostAsync("api/BookApi",content).Result;
if (message.IsSuccessStatusCode)
{
client.Dispose();
return Content("<script>alert('提交成功');location.href='/Home/Client_Get'</script>");
}
else
{
return Content("<script>alert('提交成功')</script>");
}
}
public ActionResult Client_Delete(int id)
{
Uri url = new Uri("http://localhost:52444");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.DeleteAsync("api/BookApi/"+id).Result;
if (message.IsSuccessStatusCode)
{
client.Dispose();
return Content("<script>alert('删除成功');location.href='/Home/Client_Get'</script>");
}
else
{
return Content("<script>alert('删除成功')</script>");
}
}
public ActionResult Client_Put(int id)
{
//下拉列表
day20_BookEntities db = new day20_BookEntities();
var selectlist = db.TypeInfo;
SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
ViewBag.SelectList = list2;
var data = db.Book.Where(T => T.BookID == id).FirstOrDefault();
var result = new OrderInfo()
{
BookID = data.BookID,
BookName = data.BookName,
BookNum = data.BookNum,
BookPrice = data.BookPrice,
BookTid = data.BookTid,
};
return View(result);
}
[HttpPost]
public ActionResult Client_Put(Book o)
{
Uri url = new Uri("http://localhost:52444");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent("{BookID:'" + o.BookID + "',BookName:'" + o.BookName + "',BookTid:'" + o.BookTid + "',BookNum:'" + o.BookNum + "',BookPrice:'" + o.BookPrice + "'}");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage message = client.PutAsync("api/BookApi/"+o.BookID ,content).Result;
if (message.IsSuccessStatusCode)
{
client.Dispose();
return Content("<script>alert('提交成功');location.href='/Home/Client_Get'</script>");
}
else
{
return Content("<script>alert('提交成功')</script>");
}
}
转载于:https://www.cnblogs.com/jpp666/p/7883084.html