How to cache static content using ASP.NET 5 and MVC 6?

Vi100

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.

KenL

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
    {
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Clear output cache in MVC 6 / ASP.NET 5

HttpRuntime.Cache Equivalent for asp.net 5, MVC 6

Serving static files in ASP.NET 5 MVC 6

How to create a response message and add content string to it in ASP.NET 5 / MVC 6

How to access cache in ASP.NET 5 MVC?

Using sessions in ASP.NET 5 (MVC 6)

How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)?

ASP.NET 5 (MVC6) How to seed users

How to register a global filter with mvc 6, asp.net 5

Implementing static files cache in asp.net mvc

How to keep using ModelState with RedirectToAction in ASP.NET MVC 6?

Localization in ASP.NET 5 MVC 6

how to edit in asp.net mvc 5 using view model?

ASP.NET MVC: Programmatically set HTTP headers on static content

Run ASP.NET 5 (MVC 6) using .NET Core on IIS

Using libraries with ASP.Net MVC 6

How to Use Entity Framework 6.x in Asp.Net 5 (MVC 6)

How can I return a response in ASP.NET Core MVC middleware using MVC's content negotiation?

How to switch to full .NET stack in ASP.NET 5 MVC 6 project

Migrating ASP.NET MVC 5 bundling "versions" to MVC 6

Differentiate between MVC and WebAPI in ASP.NET 5 / MVC 6

Displaying JSON data acquired from a WebRequest in ASP.NET 5 using MVC 6

How to register middle-ware for a Custom Asp.Net Identity Storage Provider using ASP-5 / MVC-6 / Identity 3

How to get the File's Content Type in asp.net mvc 5

Configure cors to allow all subdomains using ASP.NET Core (Asp.net 5, MVC6, VNext)

How to Update Model in ASP NET MVC 6?

Conditional Cache using OutputCacheAttribute in Asp.net MVC 4

InvalidOperationException in Asp.Net MVC while using In-Memory Cache