开发者

Anyway to check whether the page is run in production or dev or test in .net c#?

开发者 https://www.devze.com 2023-04-03 12:49 出处:网络
Does anyone know how can I check whether the page is run in production, dev or test in .net c#? in PHP I can use var_dump($_SERVER) to check, but somehow I have no idea how to check it in .net c#.

Does anyone know how can I check whether the page is run in production, dev or test in .net c#? in PHP I can use var_dump($_SERVER) to check, but somehow I have no idea how to check it in .net c#.

I need to find out whether the page is run in production/test/dev to display the correc开发者_运维百科t logo and heading on the page


You can setup as many configuration as you want, by default, you have Debug and Release.

You can make different web.config files for different configuration, for example: web.debug.config

More information: http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

From code you can use directives to test the configuration, for example:

#if DEBUG
  bool isDebug = true;
#else
  bool isDebug = false;
#endif


One of possible solutions could be add build configuration for Test, as Production could be Release, Dev could be Debug. For test configuration use precomp dirrective like we do for debug.

TEST key you have to define in project's TEST configuration.

So somewhere on code you will use #if TEST.

If you want to use the same build in different environments, can use external configuration file, but I personally would prefer the first solution, as you esplicitly say what build result you want. With config is easy to mess up IMHO.


One way is to check the name of your database server. This depends on the server naming convention, of course. So you fetch the ConnectionString out of your web.config, parse out the DataSource and compare it against your known production box.

Another approach is to always sanitize your dev/qa boxes with a certain value in the database that conveys that it's not a production box.


Dont you connect to different database server in every environment? if no server at least the database itself should be different, have a config table with logo url, heading text and other ru.time settings and always build same binaries.

We do this and works very well. only connection string changes in app.config but we seldom redeploy the config file.


System.Environment.MachineName

This will give you the name, so as long as you know the names of the different servers this shoul work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号