3

This was previously achieved by adding some configuration to the web.config file, but now this file is to be extinguished.

I was expecting to find some methods or properties in the middleware declaration, but I haven't found:

app.UseStaticFiles();

So, which is now the procedure to cache static content as images, scripts, etc.?

Is there another middleware to do this or is this feature not implemented yet in MVC 6?

I'm looking for a way to add the cache-control, expires, etc. headers to the static content.

1
  • Please, read the question: How to CACHE static content... If you're practicing as moderator it will be good that you read all other answers and comments before marking the question as dupe. Commented Nov 23, 2015 at 17:27

2 Answers 2

3

It is all about Middleware with AspNet Core;

Add the following to your Configure method in the Startup.cs file

app.Use(async (context, next) =>
            {
                context.Response.Headers.Add("Content-encoding", "gzip");
                context.Response.Body = new System.IO.Compression.GZipStream(context.Response.Body,
                    System.IO.Compression.CompressionMode.Compress);
                await next();
                await context.Response.Body.FlushAsync();
            });

By the way for caching you would add this to the ConfigureServices method

services.AddMvc(options =>
            {
                options.CacheProfiles.Add("Default",
                    new CacheProfile()
                    {
                        Duration = 60
                    });
                options.CacheProfiles.Add("Never",
                    new CacheProfile()
                    {
                        Location = ResponseCacheLocation.None,
                        NoStore = true
                    });
            });

And decorate the control with

[ResponseCache(CacheProfileName = "Default")]
    public class HomeController : Controller
    {
...
Sign up to request clarification or add additional context in comments.

1 Comment

Is the "Default" profile used by default? How to set default profile?
0

Your title says compress, but your question body says cache. I'll assume you mean both.

Minification of css/javascript is already handled by the grunt task runner on publish. Caching and compression outside this seem like something a webserver is more suited to, rather than the application layer, so here's a great article that details the config for nginx to manage caching and compression for kestrel.

If you're using IIS, you can configure caching and compression directly on it, here's a tutorial. Considering the previous versions of MVC configured this functionality in web.config\system.Webserver which basically sets IIS config values, you can likely still use a web.config for the purposes of configuring IIS (only).

3 Comments

Thanks for the tip, I've corrected the question, I meant caching. I'm already using grunt for minification, so what I was looking for is a way to add the cache-control, expires, etc. headers to the static content. I think that this should be a framework's feature, at least now that the application is sold as something INDEPENDENT from the server. Of course, I use Kestrel as it's the imposed way, and I know how to configure IIS in the traditional way, but I wouldn't like to treat anymore with the peculiarities of each webserver, and of course I'm not going to start learning nginx now.
Whoops, I meant grunt :) You seen this?
Ummm. The question you're linking is eight months and 4 beta versions old. Are we in the same point yet regarding this? I can't believe it. The workarrounds in the answers are awful... ASP.NET 5 is now on Release Candidate, with closed functionalities, and I hope they have implemented any graceful way to set this headers and override server configurations, thats all about ASP.NET, or I've missed something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.