惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - fanrsh

Javascript's Event 的一点总结 [转]SQL Server2005 SQLCLR代码安全之权限(3) [转]SQL Server2005 SQLCLR代码安全之权限(2) [转]SQL Server2005 SQLCLR代码安全之权限(1) Remoting事件序列一:客户端触发服务器端事件 jquery入门1:简单收缩菜单 - fanrsh - 博客园 jQuery工作原理解析以及源代码示例 ASP.NET 页面生存周期概览 推荐一个快速反射调用的类 Visual Studio 2005的版本情况和新特征详细介绍 给大家一个CSS编辑工具,结合JQUERY用的很爽啊 flv视频是如何转的? codesmith学习资源 asp.net 事件验证 CSS循序渐进系列 [更新中]Lucene.net,中文分词技术 ICTCLAS研究 .net sql连接字符串详解 socket专家好文收藏 jQuery学习资源
Creating and writing ASP.NET 2.0 custom Configuration Sections
fanrsh · 2007-08-23 · via 博客园 - fanrsh

ASP.NET 2.0 has made it pretty nice to create custom configuration sections and be able to access these configuration sections via code. You can basically implement a new ConfigurationSection class. For example, I’ve built a configuration section for my Database Resource Provider like this:

    public class wwDbResourceProviderSection : ConfigurationSection

    {

        [ConfigurationProperty("connectionString",DefaultValue=""),

        Description("The connection string used to connect to the db Resource provider")]      

        public string ConnectionString

        {

            get { return this["connectionString"] as string; }

            set { this["connectionString"]  = value; }

        }

        [ConfigurationProperty("resourceTableName",DefaultValue="Localizations"),

        Description("The name of the table used in the Connection String database for localizations.")]

        public string ResourceTableName

        {

            get { return this["resourceTableName"] as string; }

            set { this["resourceTableName"] = value; }

        }

        [ConfigurationProperty("designTimeVirtualPath",DefaultValue=""),

        Description("The virtual path to the application. This value is used at design time and should be in the format of: /MyVirtual")]

        public string DesignTimeVirtualPath

        {

            get { return this["designTimeVirtualPath"] as string; }

            set { this["designTimeVirtualPath"] = value; }

        }

        public wwDbResourceProviderSection(string ConnectionString, string ResourceTableName, string DesignTimeVirtualPath)

        {

            this.ConnectionString = ConnectionString;

            this.DesignTimeVirtualPath = DesignTimeVirtualPath;

            this.ResourceTableName = ResourceTableName;

        }

        public wwDbResourceProviderSection()

        {

        }

    }

And create your ‘properties’ for the section by simply creating public properties and marking them up with a few attributes. The whole thing can then be stuck into web.config like this:

<configSections> 

  <section name="wwDbResourceProvider"

           type="Westwind.Globalization.wwDbResourceProviderSection"

  />

</configSections>  

<wwDbResourceProvider  

    connectionString="server=(local);database=Internationalization;integrated security=true;"

    resourceTableName="Localizations"

    designTimeVirtualPath="/internationalization"

    localizationFormWebPath="~/localizationadmin/localizeform.aspx"

    addMissingResources="false"

    useVsNetResourceNaming="false"

    showLocalizationControlOptions="false"

    showControlIcons="true"

/>

Nice.

However, I’ve not been able to figure out how to write data back to the config file through the ConfigurationSection interface. This class representation supports the ability to save the content, but when I try to assign a value like so:

protected void Page_Load(object sender, EventArgs e)

{

    object T = WebConfigurationManager.GetWebApplicationSection("wwDbResourceProvider") ;

    if (T != null)

    {

        wwDbResourceProviderSection Section = T as wwDbResourceProviderSection;

        Response.Write(Section.ConnectionString);

        Section.ShowControlIcons = false;           

    }

}

I get an exception that the configuration is read only.

Exception Details: System.Configuration.ConfigurationErrorsException: The configuration is read only.

As it turns out the code to write this needs to look a little differently:

protected void Page_Load(object sender, EventArgs e)

{

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

    wwDbResourceProviderSection Section = config.GetSection("wwDbResourceProvider") as wwDbResourceProviderSection;

    Section.ShowControlIcons = true;

    config.Save();

    return;

}

And this works…

As long as you’re running at least with High Trust permissions. This understandably fails with Medium trust.