【问题标题】:What is the difference between a Web application and Web API?Web 应用程序和 Web API 有什么区别?
【发布时间】:2016-06-05 15:22:11
【问题描述】:

这听起来很幼稚,但是当有人在他们的论文中提到它时,Web 应用程序和 Web API 有什么不同? 它们与功能有何不同?

【问题讨论】:

  • 大多数 Web 应用程序都返回视图和数据,但 Web API 用于以简单的方式创建完整的 HTTP 服务,只返回数据而不返回视图。

标签: asp.net-web-api web-applications


【解决方案1】:

非常简短:Web 应用程序,它是一个网站,您可以在浏览器中看到,而 Web api,它是一种服务,您可以在 Web 应用程序中使用。

另见Difference between ASP.NET MVC and ASP.NET Web API

Asp.Net MVC 用于创建返回两个视图的 Web 应用程序 和数据,但 Asp.Net Web API 用于创建完整的 HTTP 以简单的方式提供服务,只返回数据而不返回视图。

【讨论】:

  • 如果我已经将解决​​方案创建为 Asp.Net Web API,我该如何将网站创建为单独的客户端? Visual Studio 是否支持将项目模板作为客户端应用程序(网站或我应该说是网页)?
  • @JP_medevice,没有这样的项目模板,Web API可以在不同的应用程序中使用,例如:ASP.NET,HTML/JavaScript,移动应用程序,甚至桌面
【解决方案2】:

Web 应用程序用于通过视图进行人机交互,而 Web API 又名 Web 服务用于系统间交互(以编程方式交换信息)。他们交换数据。

【讨论】:

    【解决方案3】:

    网络应用程序:

    它是面向用户的端到端解决方案。这意味着,用户可以:

    • 用浏览器打开
    • 与之互动。他可以点击一些东西,经过一些处理,其结果将反映在浏览器屏幕上。人机交互

    网络 API

    仅使用 Web API,用户无法与之交互,因为它只返回数据,而不是视图。

    • 这是一个与另一个系统交互的系统
    • 它不返回视图,它返回数据
    • 它有一个端点集,可以被其他系统访问以获取它提供的数据。

    类比解释

    网络应用程序:

    假设我们有一个厨师。我们可以随时请他为我们做任何事情!假设我们请厨师为我们做汉堡。他会处理我们的请求,并会为我们提供一个汉堡。 (这就像一个 Web 应用程序;一个完整的解决方案。)

    网络 API

    现在如果我们请他给我们做“麦当劳”汉堡,他可以做饭并给我们带来吗?不! API的概念来了! (对于这个例子,假设麦当劳只给厨师外卖)

    McDonalds-Takeaways 就像一个 API。这允许其他系统(厨师)点击它并带回所需的数据。所以我们可以要求我们的解决方案(我们的厨师)来

    • 去麦当劳外卖
    • 买汉堡给我们带来

    所以发生的事情是,我们要求我们的“系统”与这个麦当劳外卖(API 系统)对话并带回我们想要的结果。

    【讨论】:

    • 这是一个很棒的类比和清晰的解释。干得好兄弟...
    • 非常准确的解释!
    【解决方案4】:

    Web 应用是在您的浏览器中运行的网站,Web Api 是服务

    【讨论】:

      【解决方案5】:

      Web API 是后端应用程序(服务器端),其中调用服务/数据库调用的实际功能正在发生以存储和检索数据。

      Web 应用程序是前端应用程序(客户端),它调用 Web API 来呈现从后端检索到的数据。

      例如:要在您的手机银行应用程序中查看帐户余额,您可以在前端查看您的帐户详细信息。但是所有利息/余额的计算都在后端进行。

      【讨论】:

        【解决方案6】:

        简单来说,Web 应用程序对请求的响应是 html、css、javascript 和浏览器可以呈现的任何内容(图形),而 Web api 返回非图形“数据”。话虽如此,我认为我们可以让 web api 像 web 应用程序一样工作,因为 html 仍然是数据。

        【讨论】:

          【解决方案7】:
          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);
                      }
                  }
          

          【讨论】:

          • 您是否将此问题提交给错误的问题?
          猜你喜欢
          • 2017-04-11
          • 2015-10-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-14
          • 2011-04-23
          • 2012-03-16
          • 2013-10-20
          • 1970-01-01
          相关资源
          最近更新 更多