0

I have a Single Page Application with a webClient and a webAPI. When I go to a view which has a table, my table is not being updated. Actually the API is only being called once upon startup of application even though it is suppose to be called each time, or what I expected to happen.

Service Code -

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 Controller -

[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);
}

I can trace it to the service, but it will not hit a break point in the controller.

mwilczynski
  • 2,824
  • 1
  • 16
  • 27
Craig
  • 1,171
  • 1
  • 18
  • 51
  • 1
    Are you sure that your subsequent calls are not simply being cached, and thus never hit the controller after the first request? Try adding [these set of response headers](http://stackoverflow.com/a/2068407/752918) on the controller's side and see if it has any effect. – Ibrahim Arief Jul 21 '15 at 23:23
  • When are the `factory.getAccountsSummary` function being called? – mrodrigues Jul 21 '15 at 23:23
  • The factory.getAccountsSummary is being called on Init() in the controller. – Craig Jul 22 '15 at 00:29

2 Answers2

1

I added this to my web.config file for the REST API project and now it works as I need it -

  <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>

Thanks everybody for pointing me in the right direction!

Craig
  • 1,171
  • 1
  • 18
  • 51
0

I suspect the REST service response is getting cached in your browser on first call. So on REST service side add headers in response not to cache it or in your request add some additional changeable parameter(like time stamp) to ensure browser will not pick up the response form cache.

Manmay
  • 755
  • 5
  • 11