我在 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
发布评论
评论(3)
相反,我会使用 Web 部署项目,以及 web.config 中的 >configSource 属性。
我会将每个组件的 web.config 分成两个文件。例如,您的输出缓存将分为
outputcache.dev.config
和outputcache.live.config
。您应该输入配置源作为开发配置文件。您的 dev.config 基本上会告诉您的应用程序您不想缓存运行 (
enableOutputCache="false"
)。然后,当您运行部署项目时,您可以设置将 dev.config 字符串替换为 live.config。
有关 configSource 和 Web 部署项目的更多讨论。
至于你的压缩过滤器问题......好吧,我只是在你的配置文件中有一个应用程序设置值。拆分配置文件后,您将拥有
appsettings.dev.config
和appsettings.live.config
。在您的开发中,您会看到类似的内容:在您的 live.config 中,是的,您已经猜到了:
然后,当您使用该属性时,您可以简单地针对 InLiveMode 应用程序设置。
仅供参考:我更喜欢某种 facade 类,所以我不处理魔术字符串在代码中,但为了简单起见,你会得到类似的内容:
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
andoutputcache.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
, andappsettings.live.config
. In your dev, you would have something like:And in your live.config, yep, you've guessed it:
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:
抱歉,.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.
本文演示如何修改或扩展 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