Facing issue while trying to Cache images js files

2019-07-20 07:20发布

问题:

Below is my Fidler.

If you pay attention to the above screenshot, js and css files are being downloaded on every refresh. Why?

I have a query about the functionality of Combres. Url.Combress can cache the css files. right? In case you remove the css file from the physical location. I get 404 error? Why? Because this file is cached. So it should not be picked from it's physical location. Instead it should be picked from cache. Correct?


Explanation

I am using MVC3. I have installed Nuget Package combres in Package Manager Console

Install-Package combres.mvc

Below is the proof of my Route Table

I have below two files in my Layout.

<link href=""~/Content/Site.css" type="text/css" />
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>

Combress.xml setting

<combres xmlns='urn:combres'>
  <filters>
    <filter type="Combres.Filters.FixUrlsInCssFilter, Combres" />
  </filters>
  <resourceSets url="~/combres.axd"
                defaultDuration="30"
                defaultVersion="auto"
                defaultDebugEnabled="auto"
                defaultIgnorePipelineWhenDebug="true"
                localChangeMonitorInterval="30"
                remoteChangeMonitorInterval="60"
                >
    <resourceSet name="siteCss" type="css">
      <resource path="~/content/Site.css" />
    </resourceSet>
    <resourceSet name="siteJs" type="js">
      <resource path="~/scripts/jquery-1.7.1.min.js" />
    </resourceSet>
  </resourceSets>
</combres>

When i execute the below path

http://localhost:2474/Home/About

I think, it is downloading the above files every time. As per my understanding, combress should Cache the images/css/js file which ever files are mentioned in the combres.xml setting.

回答1:

You need to reference the files differently. Since you are using asp.net-mvc I would recommend you also install this complementary nuget package

Install-Package combres.mvc 

And then in your master page, or wherever you want your different resources

@Url.CombresLink("siteCss")
@Url.CombresLink("siteJs")

Note : Before adding the above code lines. Add @using Combres.Mvc; in your layout Page.

This is the standard recommendation as per the documentation.

Note that you probably want to have full source in debug mode and only combine in release mode, which is a setting on your resourceSet: defaultDebugEnabled="auto"

There are other considerations but that should be enough to get started.