web.config 中的静态值
我有一个项目在两台服务器上运行。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如其他人所说,这就是 web.config 的
appSettings
部分的用途,然后可以使用 WebConfigurationManager
但是
如果您只想在测试时不发送电子邮件,那么您可以使用 web.config 来配置.NET 转储将电子邮件发送到本地目录而不是将其发送到邮件服务器
如果您的代码不覆盖默认邮件 SMTP 服务器设置,则这将起作用。
As the others have said this is what the
appSettings
section of your web.config is forWhich can then be accessed using the WebConfigurationManager
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
This will work if your code does not override the default mail SMTP server settings.
是的,你可以:
你可以使用这样的东西来获取它:
我通常使用枚举作为我的应用程序键来保持它们的强类型,这使得它们更快更容易查找,而不是通过 web.config 进行挖掘
Yes you can:
You can get it out using something like this:
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
为什么不使用
appSettings
?Why don't you use
appSettings
?您可以使用 Web.Config 来执行此操作,使用 appSetting (ConfigurationManager.AppSetting["Key"])
或者,如果您的应用程序在测试服务器上以调试模式运行,您可以执行此操作,
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,
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
如果您使用的是 Visual Studio 2010,我建议使用转换文件来转换 web.config。你的差异越多,这对你的帮助就越大。
高级步骤:
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:
http://msdn.microsoft.com/en-us/library/dd465318.aspx