如何使#if DEBUG在asp.net中起作用 [英] How to make #if DEBUG works in asp.net

查看:233
本文介绍了如何使#if DEBUG在asp.net中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有个问题.我有这样的代码:

Hi guys,

I have a problem. I have a code something like this:

<% #if DEBUG 
  // Do Debug
#else
  //Do Release
#endif%>



这段代码可以在我的Visual Studio上正常工作.我的意思是,当我在web.config中更改debug = true或false时,可以获得所需的结果.但是,当我在服务器上发布代码时,它总是返回发布部分!不管调试是对还是错.
谁能告诉我该怎么做才能使它起作用?

注意:我将使用Visual Studio的网站发布到未选中所有复选框的服务器上.我的意思是允许此预编译站点可更新" ,其他选项是 未选中 .



This code works fine on my visual studio. I mean when I change debug=true or false in web.config, I can get the result as desire. But when I publish the code on the server, It always return the release part! no matter debug is true or false.
Can any one please tell me what should I do to make it works?

Note: I publish my website with Visual Studio to the server with all checkboxes not selected. I mean "Allow this precompiled site to be updatable" and the other options are unchecked.

推荐答案

#DEBUG预处理程序指令由VS添加.您可以查看此内容,并通过构建"标签上的属性"页面添加自己的内容->定义DEBUG常量.

但是,您不应在生产环境中运行调试代码.期间
The #DEBUG pre-processor directive is added by VS. You can view this, and add your own via the properties page on the build tab -> Define DEBUG constant.

However, you SHOULD NOT be running debug code in a production environment. Period


您可以通过检查httpcontext在Web项目中进行调试检查.在web.config中设置debugging ="true".当您要检查是否已启用调试功能时,不必像这样包装代码:

You can do debugging checks in web projects by checking the httpcontext. Set the debugging="true" in web.config. When you want to check for debugging being enabled, then instead of wrapping up your code like this:

#if DEBUG

#else

#end if




做这样的事情:




do something like this:

if (!HttpContext.Current.IsDebuggingEnabled)
{
    // Release
}
else
{
    // Debug
}


好,
我想我找到了问题,
当我在发布网站时取消选中允许此预编译的站点可更新"时,这意味着我已经为页面和代码隐藏发布了已编译的代码.因此,所有这些编译器选项现在都已编译.因此,将Debug更改为true或false不会再次编译整个网站. (没有要编译的代码),因此
Ok guys,
I think I found the problem,
When I uncheck "Allow this precompiled site to be updatable" while publishing the website, It means that I have puiblished the compiled codes for both pages and codebehinds. So all those compiler options now have been compiled. So changing the Debug to true or false, will not compile whole the website again. ( There is no code there to be compiled) and thus
#if DEBUG ... 

不起作用! (因为它是使用

does not work! (Because it is compiled with

Debug=true

编译的)
如果您有其他想法,请告诉我.
再次谢谢你们.

)
Please let me know if you think something else.
Thank you guys again.


这篇关于如何使#if DEBUG在asp.net中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆