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

发布于 2024-08-19 03:40:06 字数 733 浏览 2 评论 0 原文

我在 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 时,应忽略装饰属性 - 我想启用调试模式,并且在调试模式下不应发生压缩和缓存。

所以本质上这就像注释掉那些装饰属性(我现在实际上正在做的事情并且厌倦了它):

    //[OutputCache(Duration = 86400, Location = OutputCacheLocation.Client,
    //    VaryByParam = "jsPath;ServerHost")]
    //[CompressFilter]

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

感谢帮助

My actions in ASP.NET MVC controller are decorated with numerous properties like this

    [OutputCache(Duration = 86400, Location = OutputCacheLocation.Client,
        VaryByParam = "jsPath;ServerHost")]
    [CompressFilter]
    public JavaScriptResult GetPendingJavaScript(string jsPath, string serverHost)

What I would like to do is to wrap this in something like #if and #endif, and have DebugMode setting in my web.config file. When this setting would be set to true, the decorating properties should be disregarded - I want to enable debug mode and in debug mode no compression and caching should occur.

So essentially it would be like commenting out those decorating properties (what I'm actually doing now and got fed up with it):

    //[OutputCache(Duration = 86400, Location = OutputCacheLocation.Client,
    //    VaryByParam = "jsPath;ServerHost")]
    //[CompressFilter]

Obviously #if and #endif work with defined (#define) C# symbols, I couldn't find any example where this would work with other types of condition (like web.config values, etc.).

Help appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国产ˉ祖宗 2024-08-26 03:40:06

相反,我会使用 Web 部署项目,以及 web.config 中的 >configSource 属性。

我会将每个组件的 web.config 分成两个文件。例如,您的输出缓存将分为 outputcache.dev.configoutputcache.live.config。您应该输入配置源作为开发配置文件。

您的 dev.config 基本上会告诉您的应用程序您不想缓存运行 (enableOutputCache="false")。

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

有关 configSource 和 Web 部署项目的更多讨论

至于你的压缩过滤器问题......好吧,我只是在你的配置文件中有一个应用程序设置值。拆分配置文件后,您将拥有 appsettings.dev.configappsettings.live.config。在您的开发中,您会看到类似的内容:

<add key="InLiveMode" value="false" />

在您的 live.config 中,是的,您已经猜到了:

<add key="InLiveMode" value="true" />

然后,当您使用该属性时,您可以简单地针对 InLiveMode 应用程序设置。

仅供参考:我更喜欢某种 facade 类,所以我不处理魔术字符串在代码中,但为了简单起见,你会得到类似的内容:

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

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

Instead of this, I would make use of Web Deployment Projects, and the configSource attribute in the web.config.

I would split the web.config into two files for each component. For example, for your output cache would be split into outputcache.dev.config and outputcache.live.config. You should enter the config source as the dev config file.

Your dev.config would basically tell your app that you don't want to cache running (enableOutputCache="false").

Then, when you run your deployment project, you can have settings to replace the dev.config strings with live.config instead.

More discussion on configSource and Web Deployment Projects.

As for your CompressFilter problem... Well, I would simply have an app setting value in your config files. Following on from splitting the config files, you would have appsettings.dev.config, and appsettings.live.config. In your dev, you would have something like:

<add key="InLiveMode" value="false" />

And in your live.config, yep, you've guessed it:

<add key="InLiveMode" value="true" />

Then when you use the attribute, you can simply against the InLiveMode app setting.

FYI: I much prefer having some sort of facade class so I'm not dealing with magic strings in the code, but for the sake of simplicity, you'd have something like:

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

  if(inLiveMode)
  {
    //Do the compression shiznit
  }
}
深者入戏 2024-08-26 03:40:06

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

Sorry, there's nothing in .NET that will cause different parts of your code to compile based on what's in a config file at runtime.

荒人说梦 2024-08-26 03:40:06

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

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

This article demonstrates how to modify or extend your MVC filters (AOP) to cater for the situations you have described. Whereas the config files can be modified for deployment, when running in debug mode the problem still arises.

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文