Reputation: 24107
I want to do this sort of thing.
Can I define my symbol (TESTING or DEBUG etc) in my app.config file? If so can you provide an example of what the app.config would look like as I'm unsure where to start?
Edit: Added app.config to the title as I want to do this in an app.config
Update: It seems that I can only set conditional compilation constants at build time (web.config and app.config are used during runtime and only after the assembly has been compiled). To set conditional compilation constants I need to do this in the csproj file or options in msbuild.
Upvotes: 3
Views: 7901
Reputation: 7602
No, this is not possible with web application type projects.
The preprocessor defines in the app/web.config only affect code which is compiled after your website/application is running, but in the case of a web,console applications and libraries, all the code is compiled first and deployed as an assembly along with your static content and resources.
I would suggest instead creating an application setting and then doing a runtime check rather than a compile time check. This would allow you to enable the settings in the app/web.config at runtime rather than requiring a recompile of your code in order to achieve the intended effect.
Upvotes: 1
Reputation: 155250
You can define preprocessor symbols in web.config like so (source: http://social.msdn.microsoft.com/Forums/en-US/61ad1a74-ae76-47eb-86a9-1bf09f64c906/define-debug-constant-for-a-web-project )
<system.codedom>
<compilers>
<compiler
language="c#;cs;csharp"
extension=".cs"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
compilerOptions="/d:DEBUG,MYCONST"/>
</compilers>
</system.codedom>
See the compilerOptions=""
attribute, that's where you can define additional symbols that you can use in your front-end .aspx
, .ascx
and .master
files. I do not know if this works with Razor views or not. This approach also does not work for code-behind files which are compiled at design-time within the IDE, which have their own symbols.
Presumably this would also work for "code file"-type projects (i.e. "ASP.NET Websites") which do not use design-time compilation, but very few projects work on this basis anymore, so nevermind :)
Upvotes: 4
Reputation: 11308
No, the processor directives are not available in the web.config or app.config files.
Edited to add: These files are not actually compiled, and the #debug preprocessor value is used during compilation to tell the compiler what to do.
Upvotes: 2