Hani Honey
Hani Honey

Reputation: 2131

How to set custom cache response headers for virtual paths?

We have a virtual URL, /bundles. We want to be able to check, at some point during the .NET lifecycle, if the url starts with /bundles and then set headers. We've thought about using HttpCachePolicy Class and using setCacheability and setMaxAge. I'm wondering how we can apply that to any file served via the /bundles route? Where is the best place to handle this?

Upvotes: 0

Views: 143

Answers (1)

Calvijn
Calvijn

Reputation: 81

Sounds like you want a "different" caching behavior for this Route.

I assume that you have a special Controller for this Route.

If so then you could use the OutputCache Attribute on your Action Methods inside the Controller.

[OutputCache(Duration=[InSeconds], ...)]
public ActionResult YourMethod()
{
    ...
}

this will result in using the ASP.NET Cache Framework.

Optional: You could use a Profile which you set in the IIS WebSite Konfiguration, then you would have to use the Attribute with the Profile Parameter.

[OutputCache(Profile="YOUR_PROFILE")

IIS will add the associated Response Headers like Expire / Cache-Control / Last-Modified...

Also you would gain the Output Cache Feature which is a performance boost.


But if you want to get "full" Control over your Response Headers then you have to create an own IIS Handler where you override the Output Methods.

Because if you have an enabled Dynamic Compression, then the IIS will remove all Response Headers during ASP.NET lifecyle and add "needed" Response Headers after Compression which happens after ASP.NET processes.

Somewhere on the MSDN is a visualization of the IIS Cache Tiers. But you have to make a "deep" search on the MSDN. I would give you a link but that will take a longer time.. ;)

Upvotes: 1

Related Questions