开发者

Enable "Debug mode" in ASP.NET MVC app through the use of C# directives

开发者 https://www.devze.com 2022-12-18 17:17 出处:网络
My actions in ASP.NET MVC controller are decorated with numerous properties like this [OutputCache(Duration = 86400, Location = OutputCacheLocation.Client,

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 actu开发者_StackOverflowally 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


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
  }
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消