2

Context: ASP.NET MVC 3.0, .NET 4.0, C#, IIS 7

I have a long list of names (of game realms/servers). The realms are stored in a database.
I have an Action that returns the list as a JSON code.

I reference the list in my .aspx as following:

<script type="text/javascript" src='<%= Url.Action("Realms", "Data") %>'></script>

Here is an abbreviated action itself:

    public ActionResult Realms() {
        var realms = Data.GetRealms(...);
        var json = JsonSerialize(realms);
        return Content("realms = {0};".With(json), "text/javascript", Encoding.UTF8);
    }

This list changes very seldom (once a month).

Question: how can I cause this .js file be cached client side?

Details My problem is that this "file" is being downloaded on each page refresh and accounts for 20% of the traffic.

THX-1138
  • 21,316
  • 26
  • 96
  • 160

2 Answers2

4

how can I cause this .js file be cached client side?

It would be more reliable to cache it on the server by decorating the action with the [OutputCache] attribute. If you want to cache it on the client you could configure the cache cache location on the client when using this attribute which will send the proper Cache-Control HTTP response header:

[OutputCache(Location = OutputCacheLocation.Client, Duration = 20)]
public ActionResult Realms()
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • follow up question : http://stackoverflow.com/questions/5809987/asp-net-mvc-how-do-i-return-304-not-modified-status – THX-1138 Apr 27 '11 at 20:15
1

If you must cache on the client you have some options...

There are probably more options but these are the ones that come to mind.

CedricB
  • 1,157
  • 9
  • 23