在 xml 中创建您自己的设置

发布于 2024-07-11 01:44:30 字数 507 浏览 10 评论 0 原文

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

AllowUserToChangePanelLayout
AllowUserToDeleteCompany

等等...

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

你做什么,有什么缺点和喜好?

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

I'm in a ASP.NET project where I need to give several parameters to the administrator that is going to install the website, like:

AllowUserToChangePanelLayout
AllowUserToDeleteCompany

etc...

My question is, will be a good thing to add this into the web.config file, using my own configSession or add as a profile varibles? or should I create a XML file for this?

What do you do and what are the cons and favs?

I originally thought about web.config but I then realized that I should mess up with Website configurations and my own web app configuration and that I should create a different file, them I read this post and now I'm on this place... should I do this or that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送你一个梦 2024-07-18 01:44:30

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

如果你想使用 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>

See DevX for我偷的例子。

I usually use Settings - available via the project properties - Settings. These can be edited and saved in code, and I write a form / web page to edit them.

If you want to use the XML configuration, there's an attribute called file that reads external files.
You could have a web.config file and a someothername.config file. The someothername.config would have settings like:

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

And the web.config would have

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

See DevX for the example I stole.

高速公鹿 2024-07-18 01:44:30

只是为了让你们知道我做了配置器推荐的内容,但有所不同。

而不是一直询问(我需要的),因为

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

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

mySettings< /strong> 类

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="[email protected]" />
  <!-- 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);

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

just to let you guys know that I did what configurator recommended but with a twist.

instead of asking all the time (that I need) for

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

I just created a static class that would pull this values with what we call by Strongly typed values (so you don't need to remember all the values)

the mySettings class

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())
            ];
    }
}

the web.config appSettings part

<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>

the entire myApp.config file

<?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="[email protected]" />
  <!-- Send an alert when user import more than the number bellow -->
  <add key="Alerts_MaximumOnBatch" value="10" />

</appSettings>

So, now I call like this:

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

Hope that helps someone with the same problem :)

兔姬 2024-07-18 01:44:30

您还可以将配置放入设置文件中。 在您的项目中,打开属性并转到设置,它看起来
像这样

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

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

You may also put your configurations in a settings file. In your project, open Properties and go to Settings which looks
like so

To access the values in your code, use Properties.Settings.YourSettingName;

Use Properties.Settings.Default.Reload(); to refresh your settings during runtime

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文