简体   繁体   English

机器可变的web.config元素?

[英]Machine-variable web.config elements?

Is there a way, in the web.config, to specify machine-specific values? 在web.config中,是否可以指定机器特定的值? For example: in production I want the <customErrors> node to redirect to specific pages for displaying user-friendly error-handling pages, but in development, test, and staging, I want <customErrors mode="Off" /> so I can see as much detail as possible. 例如:在生产中,我希望<customErrors>节点重定向到特定页面以显示用户友好的错误处理页面,但是在开发,测试和登台中,我希望<customErrors mode="Off" />以便看到尽可能多的细节。

Is this currently possible? 目前可以吗?

With ASP.NET 2 and above, a lot of the config elements now support a " configSource " attribute, that allows you to replace the contents of that section with an external file. 在ASP.NET 2及更高版本中,许多配置元素现在都支持“ configSource ”属性,该属性使您可以用外部文件替换该部分的内容。 this could allow you to have general settings in the main web.config and then separate files for the sections you know are going to change per environment. 这样一来,您就可以在主要的web.config中进行常规设置,然后针对各个已知环境的各个部分分别设置文件。

As Nathan points out, in this instance you could use the RemoteOnly mode, which will display full errors when you are browsing the site on "localhost" - if you aren't developing on LocalHost then this won't help you. 正如Nathan所指出的,在这种情况下,您可以使用RemoteOnly模式,当您在“ localhost”上浏览站点时,它将显示完整的错误-如果您不在LocalHost上进行开发,那么这将无济于事。

VS2010 will add a new feature where you can modify the config file based on the build target, so a release or production build could automatically set this to "On", while debug or staging might set this to "Off". VS2010将添加一项新功能,您可以在其中基于生成目标修改配置文件,因此,发行版或生产版本可以将其自动设置为“打开”,而调试或暂存可能将其设置为“关闭”。

Another option is to hook into the Application_Error event in the global.asax, and if the user is logged in as some sort of admin, output the stack trace, otherwise display the error page. 另一个选择是挂接到global.asax中的Application_Error事件,如果用户以某种管理员身份登录,则输出堆栈跟踪,否则显示错误页面。

I'm writing and accepting my own answer because there isn't an answer to the question as I asked it. 我正在写并接受自己的答案,因为我提出的问题没有答案。 For the path: 对于路径:

C:\\inetput\\wwwroot

where wwwroot is relative root, my goal was to put the machine-specific instruction for <customErrors> in the inetpub directory so that XCopying new code automatically picked up established machine settings in the external config file. wwwroot是相对根,我的目标是将<customErrors>的计算机特定指令放在inetpub目录中,以便XCopying新代码自动在外部配置文件中获取已建立的计算机设置。 It might be possible to do this by editing the machine.config file, but that's too low-level of a solution (requires managers' signatures on paper before the server admins will do this). 可以通过编辑machine.config文件来执行此操作,但这只是解决方案的底层(在服务器管理员执行此操作之前,需要纸上经理的签名)。

So I'm simply going to stick with the status quo of editing the web.config files for the dev, test, and staging servers. 因此,我只是坚持为开发,测试和登台服务器编辑web.config文件的现状

<customErrors mode="RemoteOnly">将为您完成此操作。

If you make 如果你做

<customErrors mode="RemoteOnly">

then you will see detailed error messages on the Web Server machine but friendly error messages (that you will need to specfy in web.config) in all remote machines. 那么您将在Web Server计算机上看到详细的错误消息,但在所有远程计算机中都看到友好的错误消息(需要在web.config中指定)。

Isn't that what the .config files are for? 这不是.config文件的目的吗? Different configurations for different environments? 针对不同环境的不同配置? With websites, we always have different web.configs for each environment (referencing different connection strings etc. . .) Nathan Taylors answer is correct for your specific problem though. 对于网站,我们始终为每个环境使用不同的web.configs(引用不同的连接字符串等。)Nathan Taylors的回答对于您的特定问题是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM