Can we cache images, css and javascript in MVC3 th

2019-07-08 03:33发布

问题:

Can we cache images, css and javascript of any specific view (not partial view) in MVC3 through OutputCache attribute. like I want to cache my Home Controller view.I tried this but only contents are cached not images and css.

回答1:

Instead of Output Cache attribute you can use following methods to have caching of static resources:

1.Using max-age. Put below section in web.config in <configuration> -> <system.webServer> section

<staticContent>
    <!-- Tell client to cache static files for a year -->
    <clientCache cacheControlMode="UseMaxAge"
                 cacheControlMaxAge="365.00:00:00" />
</staticContent>

2.Using URL versioning. Can be done by bundling. If you are using MVC3 you could look at combres on nuget. In MVC4 or above it is build-in option: Bundling and Minification