启用";“调试模式”;在ASP.NET MVC应用程序中通过使用C#指令

启用";“调试模式”;在ASP.NET MVC应用程序中通过使用C#指令,c#,asp.net-mvc,c-preprocessor,C#,Asp.net Mvc,C Preprocessor,我在ASP.NET MVC控制器中的操作使用了许多类似这样的属性 [OutputCache(Duration = 86400, Location = OutputCacheLocation.Client, VaryByParam = "jsPath;ServerHost")] [CompressFilter] public JavaScriptResult GetPendingJavaScript(string jsPath, string serverH

我在ASP.NET MVC控制器中的操作使用了许多类似这样的属性

    [OutputCache(Duration = 86400, Location = OutputCacheLocation.Client,
        VaryByParam = "jsPath;ServerHost")]
    [CompressFilter]
    public JavaScriptResult GetPendingJavaScript(string jsPath, string serverHost)
我想做的是将其包装成类似于#if和#endif的格式,并在我的web.config文件中设置DebugMode。当此设置设置为true时,应该忽略装饰属性-我希望启用调试模式,并且在调试模式下不应发生压缩和缓存

因此,本质上,这就像是对那些装饰性建筑的评论(我现在正在做的事情,我已经厌倦了):

显然#if和#endif使用已定义的(#define)C#符号,我找不到任何其他类型条件(如web.config值等)的例子


帮助

我将使用
web.config
中的和属性,而不是这个

我会为每个组件将web.config拆分为两个文件。例如,对于您的输出缓存,它将被分为
outputcache.dev.config
outputcache.live.config
。您应该将配置源作为dev config文件输入

您的dev.config基本上会告诉您的应用程序您不想缓存正在运行的数据(
enableOutputCache=“false”

然后,当您运行部署项目时,可以使用设置将dev.config字符串替换为live.config

至于你的压缩机过滤器问题。。。好吧,我只需要在你的配置文件中设置一个应用程序设置值。在拆分配置文件之后,您将拥有
appsettings.dev.config
,以及
appsettings.live.config
。在您的dev中,您将有如下内容:

<add key="InLiveMode" value="false" />
//CompressFilter class
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  bool inLiveMode = bool.Parse(ConfigurationManager.AppSettings["InLiveMode"]);

  if(inLiveMode)
  {
    //Do the compression shiznit
  }
}

很抱歉,.NET中没有任何内容会导致代码的不同部分在运行时根据配置文件中的内容进行编译。

本文演示如何修改或扩展MVC过滤器(AOP),以满足您所描述的情况。虽然可以修改配置文件进行部署,但在调试模式下运行时,问题仍然会出现


是的,这很酷,而且很可能是一种方式,我会这样做。
<add key="InLiveMode" value="true" />
//CompressFilter class
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  bool inLiveMode = bool.Parse(ConfigurationManager.AppSettings["InLiveMode"]);

  if(inLiveMode)
  {
    //Do the compression shiznit
  }
}