How can I determine what 'mode' my site is running under?
如何确定我的网站运行的“模式”?
In this specific case, I have code in the code-behind pages that should act one way in 'release' mode - that is someone navigating there with a browser, and another way if I'm in debug mode coming from VS2008. (These are things like identifying which SQL connect string to use, whether or not to display certain error or warning messages, etc)
在这个特定的情况下,我在代码隐藏页面中有代码,它们应该在“发布”模式下单向运行 - 也就是有人在浏览器中导航,而另一种方式是如果我处于调试模式来自VS2008。 (这些类似于识别要使用的SQL连接字符串,是否显示某些错误或警告消息等)
VS2008 is configured to go through IIS for a variety of reasons (Cassini is not an option).
VS2008配置为通过IIS出于各种原因(Cassini不是一个选项)。
Searching all through the help I can't find anything but there HAS to be a way to identify how the website was started.
搜索所有帮助我找不到任何东西,但有一种方法来确定网站是如何开始的。
Thanks in advance.
提前致谢。
4 个解决方案
#1
I'm not sure what you mean. If you want to know if the application is currently being debugged, you can check the System.Diagnostics.Debugger.IsAttached
property. If you want to know if the application is compiled in debug mode, then you can do this:
我不确定你是什么意思。如果您想知道当前是否正在调试应用程序,可以检查System.Diagnostics.Debugger.IsAttached属性。如果您想知道应用程序是否以调试模式编译,那么您可以这样做:
#if DEBUG
const bool DebugMode = true;
#else
const bool DebugMode = false;
#endif
Also, you can use ConditionalAttribute:
此外,您可以使用ConditionalAttribute:
[System.Diagnostics.Conditional("DEBUG")]
public void ThisMethodWillOnlyExecuteInDebugMode()
{
}
I strongly suggest reconsidering that though - in my experience, having different release and debug behaviour is an excellent way to introduce bugs to your production code.
我强烈建议重新考虑这一点 - 根据我的经验,具有不同的发布和调试行为是将错误引入生产代码的绝佳方法。
#3
I use this method to detect whether debug is on in the compilation section:-
我使用此方法来检测编译部分中是否启用了调试: -
bool DebugModeOn()
{
System.Web.Configuration.CompilationSection configSection =
(System.Web.Configuration.CompilationSection)HttpContext.Current.GetSection("system.web/compilation");
return configSection.Debug;
}
This isn't perfect for exactly what you are asking. However since this should be false in production and true in development it may be good enough.
这并不完全符合您的要求。然而,由于这在生产中应该是错误的并且在开发中是真实的,因此它可能是足够好
Edit: Or you could simply use the context's IsDebuggingEnabled property as Olivier PAYEN points out. :P oops.
编辑:或者你可以简单地使用上下文的IsDebuggingEnabled属性,正如Olivier PAYEN指出的那样。 :P oops。
#4
I would use the
我会用的
#if DEBUG
// ...
#else
// ...
#endif
approach, however, I'd only use this for Exception catching in the "Main" method:
但是,我只会在“Main”方法中使用此方法来捕获异常:
- If debug, then don't catch (so the debugger can catch any uncaught exceptions),
- if release, then show an error page.
如果是debug,那么就不要捕获(因此调试器可以捕获任何未捕获的异常),
如果发布,则显示错误页面。
These don't change the flow of the program, just don't do that, there be dragons :)
这些不改变程序的流程,只是不要那样做,有龙:)
If you look at connection strings, for example, I'd just put them in web.config.
例如,如果你查看连接字符串,我只需将它们放在web.config中。
#1
I'm not sure what you mean. If you want to know if the application is currently being debugged, you can check the System.Diagnostics.Debugger.IsAttached
property. If you want to know if the application is compiled in debug mode, then you can do this:
我不确定你是什么意思。如果您想知道当前是否正在调试应用程序,可以检查System.Diagnostics.Debugger.IsAttached属性。如果您想知道应用程序是否以调试模式编译,那么您可以这样做:
#if DEBUG
const bool DebugMode = true;
#else
const bool DebugMode = false;
#endif
Also, you can use ConditionalAttribute:
此外,您可以使用ConditionalAttribute:
[System.Diagnostics.Conditional("DEBUG")]
public void ThisMethodWillOnlyExecuteInDebugMode()
{
}
I strongly suggest reconsidering that though - in my experience, having different release and debug behaviour is an excellent way to introduce bugs to your production code.
我强烈建议重新考虑这一点 - 根据我的经验,具有不同的发布和调试行为是将错误引入生产代码的绝佳方法。
#2
#3
I use this method to detect whether debug is on in the compilation section:-
我使用此方法来检测编译部分中是否启用了调试: -
bool DebugModeOn()
{
System.Web.Configuration.CompilationSection configSection =
(System.Web.Configuration.CompilationSection)HttpContext.Current.GetSection("system.web/compilation");
return configSection.Debug;
}
This isn't perfect for exactly what you are asking. However since this should be false in production and true in development it may be good enough.
这并不完全符合您的要求。然而,由于这在生产中应该是错误的并且在开发中是真实的,因此它可能是足够好
Edit: Or you could simply use the context's IsDebuggingEnabled property as Olivier PAYEN points out. :P oops.
编辑:或者你可以简单地使用上下文的IsDebuggingEnabled属性,正如Olivier PAYEN指出的那样。 :P oops。
#4
I would use the
我会用的
#if DEBUG
// ...
#else
// ...
#endif
approach, however, I'd only use this for Exception catching in the "Main" method:
但是,我只会在“Main”方法中使用此方法来捕获异常:
- If debug, then don't catch (so the debugger can catch any uncaught exceptions),
- if release, then show an error page.
如果是debug,那么就不要捕获(因此调试器可以捕获任何未捕获的异常),
如果发布,则显示错误页面。
These don't change the flow of the program, just don't do that, there be dragons :)
这些不改变程序的流程,只是不要那样做,有龙:)
If you look at connection strings, for example, I'd just put them in web.config.
例如,如果你查看连接字符串,我只需将它们放在web.config中。