8

I have got page which I would like to cache using the OutputCache directive. However, I am using a URL rewriter module to direct multiple URLs at this page, each with different contents.

Is there any way to use the cache the output for each URL? There are no other criteria by which I need to vary the cache results.

2 Answers 2

11

In the end this was quite simple to fix.

  1. Add the following directive to the page that needs to be cached:

    < %@ outputcache duration="600" location="Downstream" varybyparam="none" varybycustom="RawURL" %>

  2. Add this method to the global.asax file

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        switch (custom.ToUpper())
        {
            case "RAWURL":
                return context.Request.RawUrl;
    
            default:
                return "";
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

This is clever... but it's still returning a 200 instead of a 304 (Not Modified) as it would with a non-routed URL. So although it may not be generating the page again, the client still has to get it again, rather than using its local cache. Still, good question.
0

Programatically you can set Caching options using Response.Cache. You can switch on your querystring variable and depending on the case, set properties on Response.Cache appropriately.

MSDN on Cache object
Another helpful article from aspalliance.com

1 Comment

I can also vary the cache based on the request headers, but these do not change and there are no querystring variables used

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.