locked
How Would I Go About Correctly Caching My Images In This Scenario RRS feed

  • Question

  • User1349647816 posted

    I currently have my default Index() action in my HomeController that returns images in a json format with the actual physical url of the image on the server, the problem with this is that it hits the server everytime that the Index() action is called. What alternative method can I use to cache the images?

    Monday, August 31, 2020 4:44 PM

All replies

  • User753101303 posted

    Hi,

    It shouldn't change anything. If this is really an url (rather than a data url, not sure what you call a "physical url") you still end up with an http request that could be cached (and is perhaps already ?). How do you later load load the image? You are not using Ajax but rather assign that to an img.src ?

    You can configure caching for static files as shown at https://stackoverflow.com/questions/57254048/how-to-cache-static-files-in-asp-net-core

    You could use the approach shown at https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/image-tag-helper?view=aspnetcore-3.1 to reload the static file if it changed (you would include the hash or version in your urls).

    As often I would start by using F12 Network in the browser to first make sure about what is the current behavior before moving forward.

    Monday, August 31, 2020 5:56 PM
  • User711641945 posted

    Hi ShatterStar,

    the problem with this is that it hits the server everytime that the Index() action is called. What alternative method can I use to cache the images?

    For the index action is always called,just create a button when you click it then it could hit the index action.

    For cache images,not sure what is your image used to do,please share more details.

    Best Regards,

    Rena

    Tuesday, September 1, 2020 9:41 AM