web.config 中的静态值

发布于 2024-11-06 07:45:11 字数 394 浏览 4 评论 0原文

我有一个项目在两台服务器上运行。 1 台测试服务器连接到测试数据库,另一台测试服务器位于真实服务器上,具有真实数据库。

该项目的每个运行实例中唯一不同的是 web.config。

我想做的是能够在 web.config 中设置一个值,一个 bool 值,然后代码可以读取该值。如果应用程序处于测试模式,则此布尔值将为 true。我手动设置它,然后项目会将其读出,当它为真时,应用程序将发送的邮件将保留在内部,因此人们实际上不会收到邮件。我之前在 global.asax 中设置了 public static bool ,但在 Asp.net MVC 中,所有内容都内置到一个 DLL 中,因此我无法在部署的服务器上更改它在这种情况下。

这可能吗?或者还有其他好的解决方案吗?

I have a project running on 2 servers. 1 testserver with a connection to a testDB, and one on the real server, with real DB.

The only different thing in each of the running instances of this project is the web.config.

What i'd like to do is have the possibility to set a value in web.config, a bool, which then could be read by the code. This bool would be true if the app is in testing mode. i'd set it manually, the project then would read it out, and when it's true, the mails the app would send, then would be kept internal, so people dont actually get mail. I did this before with setting a public static bool in global.asax but in Asp.net MVC everything is built into one DLL, so i cant change it on the deployed server in that case.

Is this possible? or would there be a nice other solution?

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

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

发布评论

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

评论(6

素染倾城色 2024-11-13 07:45:11

正如其他人所说,这就是 web.config 的 appSettings 部分的用途

<configuration>
  <appSettings>
    <add key="isInTestMode" value="true"/>
  </appSettings>
  ...
</configuration>

,然后可以使用 WebConfigurationManager

bool isInTestMode = Boolean.Parse(WebConfigurationManager.AppSettings["isInTestMode"]);

但是

如果您只想在测试时不发送电子邮件,那么您可以使用 web.config 来配置.NET 转储将电子邮件发送到本地目录而不是将其发送到邮件服务器

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\MailDump\" />
      <network host="localhost"/>
    </smtp>
  </mailSettings>
  ...
</system.net>

如果您的代码不覆盖默认邮件 SMTP 服务器设置,则这将起作用。

As the others have said this is what the appSettings section of your web.config is for

<configuration>
  <appSettings>
    <add key="isInTestMode" value="true"/>
  </appSettings>
  ...
</configuration>

Which can then be accessed using the WebConfigurationManager

bool isInTestMode = Boolean.Parse(WebConfigurationManager.AppSettings["isInTestMode"]);

However

If you only interested in not sending emails when testing, then you can use the web.config to configure .NET to dump the emails to a local directory rather than sender them to the mail server

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\MailDump\" />
      <network host="localhost"/>
    </smtp>
  </mailSettings>
  ...
</system.net>

This will work if your code does not override the default mail SMTP server settings.

风流物 2024-11-13 07:45:11

是的,你可以:

<configuration>
  <appSettings>
    <add key="TestingMode" value="True" />
  </appSettings>
   ...
</configuration>

你可以使用这样的东西来获取它:

static public String GetWebConfigKey(string appSettingsKey)
    {
        String value = "";

        System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();

        try
        {
            value = asr.GetValue(appSettingsKey, System.Type.GetType("System.String")).ToString();
        }
        catch (KeyNotFoundException knfe)
        {
            throw new KeyNotFoundException("KeyNotFoundException occured in UtilityLibrary.WebConfig.getWebConfigKey" + knfe.Message);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return value;
    }

我通常使用枚举作为我的应用程序键来保持它们的强类型,这使得它们更快更容易查找,而不是通过 web.config 进行挖掘

Yes you can:

<configuration>
  <appSettings>
    <add key="TestingMode" value="True" />
  </appSettings>
   ...
</configuration>

You can get it out using something like this:

static public String GetWebConfigKey(string appSettingsKey)
    {
        String value = "";

        System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();

        try
        {
            value = asr.GetValue(appSettingsKey, System.Type.GetType("System.String")).ToString();
        }
        catch (KeyNotFoundException knfe)
        {
            throw new KeyNotFoundException("KeyNotFoundException occured in UtilityLibrary.WebConfig.getWebConfigKey" + knfe.Message);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return value;
    }

I typically use an Enum for my app keys to keep them strongly typed and it makes them quicker and easier to lookup rather than digging through the web.config

挽你眉间 2024-11-13 07:45:11

为什么不使用appSettings

<configuration>
    <appSettings>
        <add key="myValue" value="true"/>
    </appSettings>
        ....

Why don't you use appSettings?

<configuration>
    <appSettings>
        <add key="myValue" value="true"/>
    </appSettings>
        ....
苏佲洛 2024-11-13 07:45:11

您可以使用 Web.Config 来执行此操作,使用 appSetting (ConfigurationManager.AppSetting["Key"])

或者,如果您的应用程序在测试服务器上以调试模式运行,您可以执行此操作,

 #if (DEBUG)

 //Debug

#else

  //Live

#endif

You can use your Web.Config to do this, using appSetting (ConfigurationManager.AppSetting["Key"])

Or, if yuor app is running in debug mode on the test server you can do this,

 #if (DEBUG)

 //Debug

#else

  //Live

#endif
々眼睛长脚气 2024-11-13 07:45:11

ASP.Net WebDeploy 允许您根据部署位置自动转换 web.config。例如,它可以在部署到测试服务器时发送一个连接字符串,在部署到实时服务器时发送不同的连接字符串。

http://www.iis.net/download/WebDeploy

ASP.Net WebDeploy allows you to automatically transform your web.config based upon where you are deploying to. eg it can send one connection string when deploying to the test server and a different connection string when deploying to the live server.

http://www.iis.net/download/WebDeploy

吝吻 2024-11-13 07:45:11

如果您使用的是 Visual Studio 2010,我建议使用转换文件来转换 web.config。你的差异越多,这对你的帮助就越大。

高级步骤:

  1. 通过配置管理器创建新的构建配置
  2. 在每个新创建的配置文件中适当设置值
  3. 构建并发布
  4. 完成,永远不必记住更改多个配置文件(开发、暂存、发布等)中的值。

http://msdn.microsoft.com/en-us/library/dd465318.aspx

If you are using Visual Studio 2010 I would recommend using Transformation files to transform the web.config. The more differences you have the more this will help you.

The high level steps:

  1. Create a new build configuration through configuration manager
  2. Set the values appropriately in each newly created config file
  3. Build and publish
  4. Done and never have to remember to change values in multiple config files (development, staging, release etc).

http://msdn.microsoft.com/en-us/library/dd465318.aspx

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