Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an Expires header on a response in ASP.net core mvc

In a controller action, returning an IActionResult, how do I set an expires header?

I found this, but it seems to relate to server side caching, not simply setting a response header. I feel this should be really straightforward !

like image 209
bbsimonbb Avatar asked Jun 07 '18 14:06

bbsimonbb


People also ask

Does response header expire?

The HTTP Expires header is a response-type header, the fundamental way of controlling the caches present. The expire header contains the date and time which denotes the period where the object can stay alive.

Should I add expired headers?

Adding Expires Headers is important to reduce HTTP requests which reduces the time it take for the server to communicate with the browser. It also allows your users to reuse the cache files that have been stored in the browser to reduce the amount of files they need to download.


1 Answers

You can inside your contoller set the header:

string ExpireDate = DateTime.UtcNow.AddMinutes(60).ToString("ddd, dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
Response.Headers.Add("Expires", ExpireDate + " GMT");
like image 166
Yanga Avatar answered Oct 08 '22 11:10

Yanga