欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

Title: Creating and writing ASP.NET 2.0 custom configuration Sections

终极管理员 知识笔记 123阅读

ASP .网络2.0使得创建自定义配置部件变得非常好,并且这些配置部件可以通过代码访问。基本上可以实现一个新的配置部分类。例如,我为我的数据库资源提供者构建了一个配置部分。如此:公共类wwdbresourceprovider部分:配置部分{[配置属性(' ConnectionString '默认值=' '))、描述('用于连接数据库资源提供程序的连接字符串)]public string ConnectionString=' MsoNormal ' { get { return this[' ConnectionString ']as string;} set { this[' connectionString ']=value;} }[配置属性('资源表名'默认值='本地化'),Des

cription("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 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.

标签:
声明:无特别说明,转载请标明本文来源!