Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:11:00+00:00 2026-06-09T17:11:00+00:00

I’m trying to implement a MongoDB / Memory combined Output Cache Provider to use

  • 0

I’m trying to implement a MongoDB / Memory combined Output Cache Provider to use with MVC4. Here is my initial implementation:

public class CustomOutputCacheProvider : OutputCacheProvider
{
    public override object Get(string key)
    {
        FileLogger.Log(key);
        return null;
    }

    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        return entry;
    }

    public override void Set(string key, object entry, DateTime utcExpiry)
    {
    }

    public override void Remove(string key)
    {
    }
}

And my web config entry:

<caching>
  <outputCache defaultProvider="CustomOutputCacheProvider">
    <providers>
      <add name="CustomOutputCacheProvider" type="MyApp.Base.Mvc.CustomOutputCacheProvider" />
    </providers>
  </outputCache>
</caching>

And the usage within HomeController:

[OutputCache(Duration = 15)]
public ActionResult Index()
{
    return Content("Home Page");
}

My problem is, when I check the logfile for the keys that are requested, I see not only the request to home controller, but all other paths as well:

a2/  <-- should only log this entry
a2/test
a2/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d.jpg
a2/scripts/jquery-1.7.2.min.js

I’ve figured that I shouldn’t set the CustomOutputCacheProvider as the defaultProvider in Web.Config, what I couldn’t figure out is how to specify the cache provider that I want to use for a specific controller action.

With Asp.Net Web Pages you can accomplish it by using <%@ OutputCache Duration="60" VaryByParam="None" providerName="DiskCache" %> at the top of the page, but for MVC the only solution I could find is to override HttpApplication.GetOutputCacheProviderName Method in Global.asax.

Is there a more elegant way to accomplish this by using the [OutputCache] attribute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:11:01+00:00Added an answer on June 9, 2026 at 5:11 pm

    Is there a more elegant way to set the OutputCacheProvider using the [OutputCache] attribute?

    I think the answer is no, (well not with the current mvc4 release) since there is no relationship between implementing a custom OutputCacheProvider and decorating an action with the OutputCache attribute.

    As you discovered by implementing the custom provider and logging in the Get method you see every request made to the web server. If you were to remove the OutputCache attribute from all your actions you will still see every request in out log file. I thought the answer for this ASP.NET MVC hits outputcache for every action was pretty useful to confirm that.

    Since it looks like you only want to implement one output-cache provider then I think your only option is to not set the default provider and continue to override the GetOutputCacheProviderName implementation (as you have already mentioned). Perhaps something like this to exclude all Content, Images and Scripts

    public override string GetOutputCacheProviderName(HttpContext context)
    {
        string absolutePath = context.Request.Url.AbsolutePath;
    
        if (absolutePath.StartsWith("/Content/", StringComparison.CurrentCultureIgnoreCase)
            || absolutePath.StartsWith("/Scripts/", StringComparison.CurrentCultureIgnoreCase)
            || absolutePath.StartsWith("/Images/", StringComparison.CurrentCultureIgnoreCase))
            return base.GetOutputCacheProviderName(context);
    
        return "CustomOutputCacheProvider";
    }
    

    If you need to implement more than one output-cache provider then I guess you’ll have to implement a helper to give you the correct provider name. But here’s an example where I’ve resolved the routing data for you; where as the prev example looked directly at the url.

    public override string GetOutputCacheProviderName(HttpContext context)
    {       
        RouteCollection rc = new RouteCollection();
        MvcApplication.RegisterRoutes(rc);
        RouteData rd = rc.GetRouteData(new HttpContextWrapper(HttpContext.Current));
    
        if (rd == null)
            return base.GetOutputCacheProviderName(context);
    
        var controller = rd.Values["controller"].ToString();
        var action = rd.Values["action"].ToString();
    
        if (controller.Equals("Content", StringComparison.CurrentCultureIgnoreCase) 
            || controller.Equals("Scripts", StringComparison.CurrentCultureIgnoreCase) 
            || controller.Equals("Images", StringComparison.CurrentCultureIgnoreCase))
            return base.GetOutputCacheProviderName(context);
    
        if (controller.Equals("Account", StringComparison.CurrentCultureIgnoreCase))
            return "AccountOutputCacheProvider";
        if (controller.Equals("Something", StringComparison.CurrentCultureIgnoreCase))
            return controller + "OutputCacheProvider";
    
        return "CustomOutputCacheProvider";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.