質問

I have some default values for testing my code, but for security reasons those values must be set just when my app is running under the ASP.NET Development Server not in Production. How can I programmatically determine where my app is running?

役に立ちましたか?

解決

A technique is to add a property to your config which can be set using a coding transform file. In your web.config, "DEV" might be true. In your production transform file, your "DEV" might by false. With this technique, you can even have a value to determine if you are in Staging, Testing, Pre-Production for example. This is a technique used for changing connection strings as well.

A transform file is used to change various values in your config depending on your deployment.

Here's more reading: How to: Transform Web.config When Deploying a Web Application Project

他のヒント

That's a very poor way to determine if your application is running locally or on production. A better way is to use either runtime web.config values (with the corresponding transformation for Debug and Release modes) or even compile time conditionals (#if DEBUG ... #endif).

If by "ASP.NET Development Server" you mean the Cassini server included with Visual Studio, there's no supported way to do this.

You can either add the information to web.config as others have suggested, or you can use some unsupported technique (won't be guaranteed to work in future versions ...). E.g. inspect

System.Environment.CommandLine

which will contain "...WevDev.WebServer..." when running under Cassini.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top