4

I am writing a custom HttpModule and I need to use:

Response.Cache.SetVaryByCustom("role");

Basically, I need to cache responses based on a user's membership role. All my research has pointed me to the page:

http://msdn.microsoft.com/en-us/library/5ecf4420(v=vs.100).aspx

Where it is suggesed to override GetVaryByCustomString in Global.asax.

Is there a way I can avoid doing this in global.asax? Let's just assume I'm building a shrink wrapped application.

Alfero Chingono
  • 2,663
  • 3
  • 33
  • 54
  • Good question and I'm not sure. It's one of those methods that's been there since the early days and was not designed with this kind of extensibility in mind. So it requires overriding global app, which is not very pluggable. Maybe someone else will have some thoughts. – David Ebbo Oct 24 '12 at 17:57
  • Thanks @DavidEbbo. One thought - Is there a way to plug-in to the pipeline at the point which the `HttpApplication` is instantiated, then create an instance that overrides the `GetVaryByCustomString` method? – Alfero Chingono Oct 24 '12 at 19:25
  • That seems difficult because there can only be one application type. e.g. suppose global.asax has some code. That code is expected to be part of the HttpApplication. But there can't be both that code and your own HttpApplication. We'd need multiple inheritance :) – David Ebbo Oct 24 '12 at 23:17
  • How about a custom `OutputCacheProvider` e.g. https://github.com/maxtoroq/DiskOutputCache? – Alfero Chingono Oct 29 '12 at 02:10

1 Answers1

0

Why does a "shrinkwrapped" application preclude the Global.asax?

Try this:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom == "role")
            return Roles.GetRolesForUser().Aggregate((a,b) => a = a+b);
    }
matt-dot-net
  • 4,204
  • 21
  • 24