Craig
Craig

Reputation: 1225

Rest API is not always called

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.

Upvotes: 0

Views: 71

Answers (2)

Craig
Craig

Reputation: 1225

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!

Upvotes: 1

Manmay
Manmay

Reputation: 844

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.

Upvotes: 0

Related Questions