0

I have a customer page in an ASP.NET MVC Core application which uses an Angular controller. The controller is calling an API to get the data. When the page is first loaded, this works ok. From this page the user can navigate to another page to add a customer. When the new customer is added, the user is navigated back to the main page. But then the new customer is not shown on the main page, it looks like the API is not called again so the user is presented with old data. I set a breakpoint in the API solution to confirm that the API is not called after adding the new customer.

Loading data on the customer page:

function customerController($http) {
    var vm = this;
    vm.customers = [];

    $http.get("http://myapi")
        .then(function (response) {
            angular.copy(response.data, vm.customers);
        }, function (error) {
            vm.errorMessage = "Failed to load data: " + error;
        });
}

Redirect from the detail page to the main page (also from an Angular controller):

    vm.addCustomer = function () {

                $http.post("http://myapi", vm.currentCustomer)
                    .then(function (response) {                    
                        $window.location.href = "./Customers";
                    }, function (error) {
                        vm.errorMessage = "Failed to save new customer";
                    });    
}

The data is actually stored in the database via the API, but on the redirect the new data is not shown. When I refresh the main page, the new customer is still not shown. Should I force Angular somehow to call the API again? The constructor of the controller of the main page is called after redirect.

AT82
  • 71,416
  • 24
  • 140
  • 167
ngruson
  • 1,176
  • 1
  • 13
  • 25

1 Answers1

1

I suppose that ASP.NET Core caches your index.html file or there is another cache enabled.

I would checked the response headers in the browser dev tools to make sure that the page doesn't have cache attributes.

Or try this:

app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = context =>
        {                   
            if (context.File.Name == "index.html" ) {
                context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
                context.Context.Response.Headers.Add("Expires", "-1");
            }
        }
    });

This code is from https://stackoverflow.com/a/45329316/3710672

Iaroslav
  • 295
  • 1
  • 10