【发布时间】:2016-06-05 15:22:11
【问题描述】:
这听起来很幼稚,但是当有人在他们的论文中提到它时,Web 应用程序和 Web API 有什么不同? 它们与功能有何不同?
【问题讨论】:
-
大多数 Web 应用程序都返回视图和数据,但 Web API 用于以简单的方式创建完整的 HTTP 服务,只返回数据而不返回视图。
标签: asp.net-web-api web-applications
这听起来很幼稚,但是当有人在他们的论文中提到它时,Web 应用程序和 Web API 有什么不同? 它们与功能有何不同?
【问题讨论】:
标签: asp.net-web-api web-applications
非常简短:Web 应用程序,它是一个网站,您可以在浏览器中看到,而 Web api,它是一种服务,您可以在 Web 应用程序中使用。
另见Difference between ASP.NET MVC and ASP.NET Web API:
Asp.Net MVC 用于创建返回两个视图的 Web 应用程序 和数据,但 Asp.Net Web API 用于创建完整的 HTTP 以简单的方式提供服务,只返回数据而不返回视图。
【讨论】:
Web 应用程序用于通过视图进行人机交互,而 Web API 又名 Web 服务用于系统间交互(以编程方式交换信息)。他们交换数据。
【讨论】:
网络应用程序:
它是面向用户的端到端解决方案。这意味着,用户可以:
网络 API
仅使用 Web API,用户无法与之交互,因为它只返回数据,而不是视图。
类比解释
网络应用程序:
假设我们有一个厨师。我们可以随时请他为我们做任何事情!假设我们请厨师为我们做汉堡。他会处理我们的请求,并会为我们提供一个汉堡。 (这就像一个 Web 应用程序;一个完整的解决方案。)
网络 API
现在如果我们请他给我们做“麦当劳”汉堡,他可以做饭并给我们带来吗?不! API的概念来了! (对于这个例子,假设麦当劳只给厨师外卖)
McDonalds-Takeaways 就像一个 API。这允许其他系统(厨师)点击它并带回所需的数据。所以我们可以要求我们的解决方案(我们的厨师)来
所以发生的事情是,我们要求我们的“系统”与这个麦当劳外卖(API 系统)对话并带回我们想要的结果。
【讨论】:
Web 应用是在您的浏览器中运行的网站,Web Api 是服务
【讨论】:
Web API 是后端应用程序(服务器端),其中调用服务/数据库调用的实际功能正在发生以存储和检索数据。
Web 应用程序是前端应用程序(客户端),它调用 Web API 来呈现从后端检索到的数据。
例如:要在您的手机银行应用程序中查看帐户余额,您可以在前端查看您的帐户详细信息。但是所有利息/余额的计算都在后端进行。
【讨论】:
简单来说,Web 应用程序对请求的响应是 html、css、javascript 和浏览器可以呈现的任何内容(图形),而 Web api 返回非图形“数据”。话虽如此,我认为我们可以让 web api 像 web 应用程序一样工作,因为 html 仍然是数据。
【讨论】:
Create :
public IHttpActionResult GetAllProduct()
{
IList<product> pro = null;
using (var ctx = new TestMVCEntities())
{
pro = ctx.products.ToList();
}
if (pro.Count == 0)
{
return NotFound();
}
return Ok(pro);
}
public IHttpActionResult PostNewProduct(product pro)
{
using (var ctx = new TestMVCEntities())
{
ctx.InUPProduct(pro.pid,pro.pname,pro.pprice);
ctx.SaveChanges();
}
return Ok();
}
public IHttpActionResult PutOldProduct(product pro)
{
using (var ctx = new TestMVCEntities())
{
product c = (from x in ctx.products
where x.pid == pro.pid
select x).First();
if (c != null)
{
c.pname = pro.pname;
c.pprice = pro.pprice;
ctx.SaveChanges();
}
else
{
return NotFound();
}
}
return Ok();
}
public IHttpActionResult Delete(int id)
{
using (var ctx = new TestMVCEntities())
{
var pro = ctx.products
.Where(s => s.pid == id)
.FirstOrDefault();
ctx.Entry(pro).State = System.Data.Entity.EntityState.Deleted;
ctx.SaveChanges();
}
return Ok();
}
Consume :
public JsonResult GetProductsData()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:44350/api/");
//HTTP GET
var responseTask = client.GetAsync("product");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsAsync<IList<product>>();
readTask.Wait();
var alldata = readTask.Result;
var rsproduct = from x in alldata
select new[]
{
Convert.ToString(x.pid),
Convert.ToString(x.pname),
Convert.ToString(x.pprice),
Convert.ToString(x.pimage),
Convert.ToString(x.pisdemand),
Convert.ToString(x.pcname),
Convert.ToString(x.psupply)
};
return Json(new
{
aaData = rsproduct
},
JsonRequestBehavior.AllowGet);
}
else //web api sent error response
{
var pro = Enumerable.Empty<product>();
return Json(new
{
aaData = pro
},
JsonRequestBehavior.AllowGet);
}
}
}
public JsonResult InupProduct(string id,string pname, string pprice)
{
try
{
product obj = new product
{
pid = Convert.ToInt32(id),
pname = pname,
pprice = Convert.ToDecimal(pprice)
};
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:44350/api/product");
//HTTP POST
var postTask = client.PostAsJsonAsync<product>("product", obj);
postTask.Wait();
var result = postTask.Result;
if (result.IsSuccessStatusCode)
{
return Json(1, JsonRequestBehavior.AllowGet);
}
else
{
return Json(0, JsonRequestBehavior.AllowGet);
}
}
/*context.InUPProduct(Convert.ToInt32(id),pname,Convert.ToDecimal(pprice));
return Json(1, JsonRequestBehavior.AllowGet);*/
}
catch (Exception ex)
{
return Json(0, JsonRequestBehavior.AllowGet);
}
}
public JsonResult deleteRecord(int ID)
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:44350/api/product");
//HTTP DELETE
var deleteTask = client.DeleteAsync("product/" + ID);
deleteTask.Wait();
var result = deleteTask.Result;
if (result.IsSuccessStatusCode)
{
return Json(1, JsonRequestBehavior.AllowGet);
}
else
{
return Json(0, JsonRequestBehavior.AllowGet);
}
}
/* var data = context.products.Where(x => x.pid == ID).FirstOrDefault();
context.products.Remove(data);
context.SaveChanges();
return Json(1, JsonRequestBehavior.AllowGet);*/
}
catch (Exception ex)
{
return Json(0, JsonRequestBehavior.AllowGet);
}
}
【讨论】: