English 中文(简体)
Output cache for multi-tenant application, varying by hostname and culture
原标题:

I have a multi-tenant application in ASP.NET MVC. The instance of the application that will be served is function of the hostname alone (something along the lines of stackexchange, I suppose).

Each instance of the application might have a different culture setting (even "auto", to read the browser s language and try to use it), and will be localized accordingly.

In this situation, I d like to do some output caching on some of my actions. So, my questions are:

  1. What are the possibilities to achieve output caching of a multi-tenant ASP.NET MVC application, if the output depends exclusively on the hostname (ie, ignoring the localization requirement)?

  2. Same as (1), but now considering that the output depends on the culture settings as well?

  3. Same as (2), but considering that the output might vary with parameters that were passed to the action?

In this case, I m considering that all the sites run from a single IIS website.

最佳回答

I ve just figured out how to achieve this.

Simply use the VaryByHeader property, set to "host". There are many possibilities to do so.

Method 1

Use the OutputCacheAttribute passing all the needed configuration elements, including VaryByHeader:

public class HomeController : Controller
{  
    [OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
    public ActionResult Index() { /* ... */ }
}

Method 2.

Or you could set it to a profile on the Web.config:

<?xml version="1.0"?>
<configuration>
  <!-- ... -->
  <system.web>
    <!-- ... -->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear/>
          <add name="Multitenant" 
               enabled="true"
               duration="3600"
               varyByHeader="host"
               varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
</configuration>

Then use it:

public class HomeController : Controller
{  
    [OutputCache(CacheProfile = "Multitenant")]
    public ActionResult Index() { /* ... */ }
}

Method 3.

Or you can subclass the OutputCacheAttribute and use it:

public sealed class MultitenantOutputCacheAttribute : OutputCacheAttribute
{
    public MultitenantOutputCacheAttribute()
    {
        VaryByHeader = "host";
        VaryByParam = "none";
        Duration = 3600;
    }
}

Then use it:

public class HomeController : Controller
{  
    [MultitenantOutputCache]
    public ActionResult Index() { /* ... */ }
}
问题回答

In case people land on this page and are looking for the equivalent in asp.net 2.x. The attribute will look like this:

[ResponseCache(Duration = 30, Location = ResponseCacheLocation.Any, VaryByHeader = "host", VaryByQueryKeys = new string[] { "*" })]

And you will need the middleware added. You need this nuget package and this code:

public void ConfigureServices(IServiceCollection services)
{
    //stuff before...

    services.AddResponseCaching();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    //stuff after...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //stuff before...

    app.UseResponseCaching();

    //stuff after...
}

More details here





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签