首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从web.config检索<caching>元素

从web.config检索<caching>元素
EN

Stack Overflow用户
提问于 2011-02-28 21:56:15
回答 1查看 359关注 0票数 1

我一直试图从我的web.config中获取缓存元素,但到目前为止都失败了。

使用此代码时:

代码语言:javascript
复制
Configuration conf  = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);

我能够得到web.configh文件。当我使用

代码语言:javascript
复制
conf.GetSection("system.web/membership");

我成功地进入了会员区。

当我使用

代码语言:javascript
复制
conf.GetSection("system.web/caching");

我得到null。

有什么想法吗?

下面是web.config的一部分:

代码语言:javascript
复制
    <system.web>
<caching>
  <sqlCacheDependency enabled="true" pollTime="1000">
    <databases>
      <clear />
      <add name="Tests" pollTime="1000" connectionStringName="TestsConnectionString"/>          
    </databases>        
  </sqlCacheDependency>      
</caching>
        <authentication mode="Forms">
        <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
    </authentication>
    <membership>
        <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
        </providers>
    </membership>

……

EN

回答 1

Stack Overflow用户

发布于 2018-04-23 14:42:20

您是否正确地将返回类型转换为OutputCacheSection?

代码语言:javascript
复制
const string outputCacheKey = "system.web/caching/outputCache";
var outputCacheSection = ConfigurationManager.GetSection(outputCacheKey) as OutputCacheSection;

现在假设您想要在第一个提供者节点中获得一个名为connectionString的属性。

代码语言:javascript
复制
<caching>
  <outputCache enableOutputCache="true" defaultProvider="MyRedisOutputCache">
    <providers>
      <add name="MyRedisOutputCache" connectionString="myapp.redis.cache.windows.net:6380,password=hellopassword,ssl=True,abortConnect=False" type="Microsoft.Web.Redis.RedisOutputCacheProvider,Microsoft.Web.RedisOutputCacheProvider" />
    </providers>
  </outputCache>
</caching>

你可以这样做

代码语言:javascript
复制
string defaultProviderName = outputCacheSection.DefaultProviderName;
ProviderSettings ps = outputCacheSection.Providers[defaultProviderName];
var cs = ps.Parameters["connectionString"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5142882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档