简体   繁体   English

我可以在web.config或app.config中设置条件编译常量吗?

[英]Can I set a conditional compilation constant in a web.config or app.config, if so how?

I want to do this sort of thing. 我想做这种事情。

Can I define my symbol (TESTING or DEBUG etc) in my app.config file? 我可以在我的app.config文件中定义我的符号(测试或调试等)吗? If so can you provide an example of what the app.config would look like as I'm unsure where to start? 如果是这样,您可以提供一个示例示例,说明我不确定从哪里开始时app.config的外观。

Edit : Added app.config to the title as I want to do this in an app.config 编辑 :将app.config添加到标题,因为我想在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). 更新 :似乎我只能在构建时设置条件编译常量(在运行时以及仅在汇编已编译之后才使用web.config和app.config)。 To set conditional compilation constants I need to do this in the csproj file or options in msbuild. 要设置条件编译常量,我需要在csproj文件或msbuild中的选项中执行此操作。

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 ) 您可以像这样在web.config中定义预处理器符号(来源: http : //social.msdn.microsoft.com/Forums/en-US/61ad1a74-ae76-47eb-86a9-1bf09f64c906/define-debug-constant-for-一个网络项目

<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. 请参阅compilerOptions=""属性,在这里您可以定义可以在前端.aspx.ascx.master文件中使用的其他符号。 I do not know if this works with Razor views or not. 我不知道这是否适用于Razor视图。 This approach also does not work for code-behind files which are compiled at design-time within the IDE, which have their own symbols. 这种方法也不适用于在设计时在IDE内编译的,具有自己符号的代码隐藏文件。

Presumably this would also work for "code file"-type projects (ie "ASP.NET Websites") which do not use design-time compilation, but very few projects work on this basis anymore, so nevermind :) 大概这也适用于“代码文件”类型的项目(即“ ASP.NET网站”),这些项目不使用设计时编译,但是很少有项目在此基础上工作,所以不要紧:)

No, the processor directives are not available in the web.config or app.config files. 不,处理器指令在web.config或app.config文件中不可用。

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. 编辑添加:这些文件实际上并未编译,并且在编译期间使用#debug预处理器值来告诉编译器该做什么。

No, this is not possible with web application type projects. 不,这对于Web应用程序类型项目是不可能的。

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. 在app / web.config中定义的预处理器仅影响在您的网站/应用程序运行之后编译的代码,但是对于Web,控制台应用程序和库,在所有情况下,所有代码都首先编译并作为程序集与您的静态内容和资源。

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. 这将允许您在运行时启用app / web.config中的设置,而不需要重新编译代码即可达到预期的效果。

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

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