【问题标题】:create your own settings in xml在 xml 中创建自己的设置
【发布时间】:2010-09-28 14:38:37
【问题描述】:

我在一个 ASP.NET 项目中,我需要向要安装网站的管理员提供几个参数,例如:

AllowUserToChangePanelLayout
AllowUserToDeleteCompany

等等……

我的问题是,使用我自己的 configSession 或添加为配置文件变量将其添加到 web.config 文件中会是一件好事吗?还是我应该为此创建一个 XML 文件?

你是做什么的,有什么缺点和优点?

我最初考虑的是 web.config,但后来我意识到我应该搞乱网站配置和我自己的 Web 应用程序配置,我应该创建一个不同的文件,我阅读了 this post,现在我在这个地方...我应该这样做还是那样?

【问题讨论】:

    标签: c# asp.net configuration-files


    【解决方案1】:

    我通常使用设置 - 可通过项目属性 - 设置。这些可以编辑并保存在代码中,我编写了一个表单/网页来编辑它们。

    如果你想使用 XML 配置,有一个名为 file 的属性可以读取外部文件。 你可以有一个 web.config 文件和一个 someothername.config 文件。 someothername.config 的设置如下:

    <appSettings>
        <add key="ConnString" value="my conn string" />
        <add key="MaxUsers" value="50" />
    </appSettings>
    

    而且 web.config 会有

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings file="ExternalWeb.config">
            <add key="MyKey" value="MyValue" />
        </appSettings>
    </configuration>
    

    我偷的例子见DevX

    【讨论】:

    • 好主意 :) 我刚刚添加,测试它,就像一个魅力!谢谢
    • 请注意外部文件已被缓存。如果您对其进行更改,则必须重新启动应用程序才能重新加载设置。
    • @JohnSaunders 你确定吗?我认为这适用于所有部分。
    • 只有 appSettings 有 file 属性。其他一些部分具有configSource 属性。这不是一个要求。此外,file 属性具有不同的语义。如果文件不存在,这不是错误 - 设置只是不生效。 appSettings 的东西是给 user.config 的。
    【解决方案2】:

    只是为了让你们知道我做了configurator 推荐的事情,但有一点不同。

    而不是一直要求(我需要的)

    System.Configuration.ConfigurationManager.AppSettings["myKey"];
    

    我刚刚创建了一个静态类,它会使用我们所谓的强类型值提取这些值(因此您不需要记住所有值)

    mySettings

    public static class mySettings
    {
        public enum SettingsType
        { UserPermitions, WebService, Alerts }
        public enum SectionType
        { AllowChangeLayout, AllowUserDelete, MaximumReturnsFromSearch, MaximumOnBatch, SendTo }
    
        public static String GetSettings(SettingsType type, SectionType section)
        {
            return
                ConfigurationManager.AppSettings[
                    String.Format("{0}_{1}",
                        Enum.Parse(typeof(SettingsType), type.ToString()).ToString(),
                        Enum.Parse(typeof(SectionType), section.ToString()).ToString())
                ];
        }
    }
    

    web.config appSettings 部分

    <configuration>
      <appSettings file="myApp.config">
        <add key="UserPermitions_AllowChangeLayout" value="" />
        <add key="UserPermitions_AllowUserDelete" value="" />    
    
        <add key="WebService_MaximumReturnsFromSearch" value="" /> 
    
        <add key="Alerts_SendTo" value="" />
        <add key="Alerts_MaximumOnBatch" value="" />
      </appSettings>
    </configuration>
    

    整个 myApp.config 文件

    <?xml version="1.0" encoding="utf-8" ?>
    <!--
    ###
    ### This file serves the propose of a quick configuration.
    ### Administrator can either change this values directly or use the 
    ###   Settings tab in the application.
    ###
    -->
    <appSettings>
    
      <!-- *** User Access Configuration *** -->
      <!-- Allow user to change the panels layout {1: Yes} {0: No} -->
      <add key="UserPermitions_AllowChangeLayout" value="1" />
      <!-- Allow user to delete a company fro monitoring -->
      <add key="UserPermitions_AllowUserDelete" value="1" />
    
      <!-- *** Web Service configuration *** -->
      <!-- Maximum responses from the search service -->
      <add key="WebService_MaximumReturnsFromSearch" value="10" />
    
      <!-- *** Allerts configuration *** -->
      <!-- Send the alerts to the email writeen below -->
      <add key="Alerts_SendTo" value="bruno.in.dk@gmail.com" />
      <!-- Send an alert when user import more than the number bellow -->
      <add key="Alerts_MaximumOnBatch" value="10" />
    
    </appSettings>
    

    所以,现在我这样称呼:

    p.value = mySettings.GetSettings(
                 mySettings.SettingsType.WebService, 
                 mySettings.SectionType.MaximumReturnsFromSearch);
    

    希望能帮助遇到同样问题的人:)

    【讨论】:

    • 这看起来不太可扩展,因为您必须始终保持枚举更新...如果您想要命名属性,.settings 资源文件可能更实用。它无法从应用程序外部维护,但您可以将其用作 .config 键字符串的强类型映射。
    • 顺便说一下(题外话),不应该是“权限”而不是“权限”吗?
    • 哇,Enum.Parse(typeof(SettingsType) , type.ToString()).ToString() 的意义何在?不是和type.ToString()一样吗?
    【解决方案3】:

    您也可以将配置放在设置文件中。在您的项目中,打开“属性”并转到“设置” like so

    要访问代码中的值,请使用Properties.Settings.YourSettingName;

    在运行时使用Properties.Settings.Default.Reload(); 刷新您的设置

    【讨论】:

    • 我的类库设置总是使用默认值,即使我在配置文件中更改了它们。在设置对我有用之前调用 Reload(),即使我没有在运行时更改设置。你知道为什么需要调用 Reload() 吗?
    猜你喜欢
    • 2014-07-13
    • 2021-06-16
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多