ASP.NET MVC输出缓存multinenant申请,由主机名和文化各不相同 [英] ASP.NET MVC output cache for multinenant application, vary by hostname and culture

查看:124
本文介绍了ASP.NET MVC输出缓存multinenant申请,由主机名和文化各不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC一个的多租户应用程序的。将要服务的应用程序的实例是对的主机名的单独功能(沿stackexchange线的东西,我想)。

I have a multitenant 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. 什么是在的可能性,实现多租户ASP.NET MVC应用程序的输出缓存,如果输出完全取决于主机名(即忽略本地化要求)?

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

同(1),但现在考虑到在输出取决于文化设置以及

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

同(2),但考虑到在输出可能与参数被传递到操作的变化?

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

在这种情况下,我考虑到所有的网站从一个单一的IIS网站上运行(以回答评论)

In this case, I'm considering that all the sites run from a single IIS website (to answer to a comment).

感谢。

推荐答案

我刚刚想出如何实现这一目标。

I've just figured out how to achieve this.

只需使用 VaryByHeader 属性设置为主机。有很多可能性这样做。

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

使用 OutputCacheAttribute 传递所有需要的配置元素,包括 VaryByHeader

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() { /* ... */ }
}

方法2

或者你可以将其设置为上的Web.config配置文件:

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>

然后使用它:

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

方法3。

或者,你也可以继承的 OutputCacheAttribute 并使用它:

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

然后使用它:

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

这篇关于ASP.NET MVC输出缓存multinenant申请,由主机名和文化各不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆