4

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

AllowUserToChangePanelLayout
AllowUserToDeleteCompany

ETC...

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

你做什么,有什么缺点和最喜欢的?

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

4

3 回答 3

13

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

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

于 2008-12-28T12:56:52.680 回答
2

只是为了让你们知道我做了配置器推荐的东西,但有一点不同。

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

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

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

于 2008-12-28T14:13:55.943 回答
1

您也可以将配置放在设置文件中。在您的项目中,打开属性并转到设置, 如下所示

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

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

于 2016-02-18T09:21:32.777 回答