2016-09-30 128 views
0

任何人都可以請提供給我關於如何進行的輸入。以下要求。網頁表單和網頁API整合

我有在VS專業2015年開發的Web應用程序,並有單獨的Web API應用程序。到目前爲止,現有的Web應用程序在3層架構中實現。現在我們想要使用不帶3層架構的Web API調用來實現新頁面。

首先,我想在我的web應用程序中創建一個基礎結構/體系結構來調用Web API應用程序。這樣所有新的頁面請求都會通過這個基礎結構/體系結構並調用Web API。

請幫助我提供寶貴意見/建議。

預先感謝您。

+0

使用角度JS或jQuery的通過AJAX來獲取數據。服務器初始頁面爲靜態html文件。 – zaitsman

回答

0

因爲我們正在使用API除非有人願意使用ReactJsAngularJs,否則不需要創建複雜的基礎架構/體系結構。 對於他們來說,它甚至不是強制性的,但它可以讓你保持代碼清潔。您只需簡單地使用JavaScript調用API即可 例如,您有一個API控制器方法並希望訪問它。

產品

#region Get Method 
    // GET api/product 

    [Queryable] 

    [Route("Products")] 
    [Route("All")] 

    public HttpResponseMessage Get() 
    { 
     try 
     { 
      var products = _productServices.GetAllProducts(); 
      var productEntities = products as List<ProductEntity> ?? products.ToList(); 
      if (productEntities.Any()) 
      { 
       return Request.CreateResponse(HttpStatusCode.OK, productEntities); 
      } 

      throw new ApiDataException(1000,"No Products Found",HttpStatusCode.NotFound); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

    #endregion 

簡單方法只需調用尊重方法從阿賈克斯這樣

$(document).ready(function() { 
var a = "Test"; 
$.ajax({ 
    url: "v1/products/product/All", 
    type: "GET", 
    data: { a : a }, 
    success: function (response) { 
     alert(response); 
    }, 
    error: function (response) { 
     alert(response); 
    } 
});}); 

希望這有助於