2015-07-21 53 views
0

我有一個帶有webClient和webAPI的單頁面應用程序。當我去一個有表格的視圖時,我的表格沒有被更新。實際上,只有在應用程序啓動時纔會調用API,即使它每次都會被調用,或者我期望發生的事情。Rest API並不總是被調用

服務代碼 -

function getPagedResource(baseResource, pageIndex, pageSize) { 
    var resource = baseResource; 
    resource += (arguments.length == 3) ? buildPagingUri(pageIndex, pageSize) : ''; 
    return $http.get(serviceBase + resource).then(function (response) { 
     var accounts = response.data; 
     extendAccounts(accounts); 
     return { 
      totalRecords: parseInt(response.headers('X-InlineCount')), 
      results: accounts 
     }; 
    }); 
} 

factory.getAccountsSummary = function (pageIndex, pageSize) { 
    return getPagedResource('getAccounts', pageIndex, pageSize); 
}; 

API控制器 -

[Route("getAccounts")] 
[EnableQuery] 
public HttpResponseMessage GetAccounts() 
{ 
    int totalRecords; 
    var accountsSummary = AccountRepository.GetAllAccounts(out totalRecords); 
    HttpContext.Current.Response.Headers.Add("X-InlineCount", totalRecords.ToString()); 
    return Request.CreateResponse(HttpStatusCode.OK, accountsSummary); 
} 

我可以跟蹤到服務,但它不會打在控制器一個破發點。

+1

您確定您的後續調用不是簡單地被緩存,因此在第一次請求後從未擊中控制器?嘗試在控制器端添加[這組響應頭](http://stackoverflow.com/a/2068407/752918),看看它是否有任何作用。 –

+0

何時調用'factory.getAccountsSummary'函數? – mrodrigues

+0

在控制器的Init()上調用了factory.getAccountsSummary。 – Craig

回答

1

我將此添加到我的web.config文件REST API項目,現在它的作品,因爲我需要它 -

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Cache-Control" value="no-cache, no-store, must-revalidate" /> 
     <!-- HTTP 1.1. --> 
     <add name="Pragma" value="no-cache" /> 
     <!-- HTTP 1.0. --> 
     <add name="Expires" value="0" /> 
     <!-- Proxies. --> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 

謝謝大家指着我在正確的方向!

0

我懷疑REST服務響應在第一次調用時會緩存在瀏覽器中。因此,在REST服務端添加標題以響應不緩存或在請求中添加一些額外的可更改參數(如時間戳)以確保瀏覽器不會選取響應表單緩存。

+0

你能給我一些示例代碼嗎? – Craig

相關問題