为asp.net核心配置Smidge库

问题描述 投票:0回答:2

斯科特汉塞尔曼今天blogged关于Smidge。我认为图书馆非常好,我正在评估图书馆。

我喜欢在示例中为Debug和Production定义逻辑的选项:

bundles.CreateJs("test-bundle-3", "~/Js/Bundle3")
   .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
      .ForDebug(builder => builder
         .EnableCompositeProcessing()
         .EnableFileWatcher()
         .SetCacheBusterType<AppDomainLifetimeCacheBuster>()
         .CacheControlOptions(enableEtag: false, cacheControlMaxAge: 0))
      .Build()

但是我无法找出Debug / Production的定义。有没有办法告诉系统何时进行调试以及何时处于生产模式?

似乎版本只能在配置中定义。

"smidge": {
  "dataFolder" : "App_Data/Smidge",
  "version" : "1"
}  

是否有选项在代码中定义版本?

c# asp.net-core .net-core smidge
2个回答
1
投票

有没有办法告诉系统何时进行调试以及何时处于生产模式?

这里有文档介绍:https://github.com/Shazwazza/Smidge/wiki/Rendering#debugging,它基于html标签的debug="true"属性。

似乎版本只能在配置中定义。

该版本由Smidge.Cache.ICacheBuster在Smidge中控制。目前有2个实现:

/// <summary>
/// Based on a static string specified in config
/// </summary>
public class ConfigCacheBuster : ICacheBuster

/// <summary>
/// Creates a cache bust value for the lifetime of the app domain
/// </summary>
/// <remarks>
/// Essentially means that all caches will be busted when the app restarts
/// </remarks>
public class AppDomainLifetimeCacheBuster : ICacheBuster

因此,可以指定其中一个或实现自己的。如果您实现自己的,则需要将其添加到容器中,如:

services.AddSingleton<ICacheBuster, MyCacheBuster>();

然后,您可以为捆绑包指定选项(有多种方法可以执行此操作),例如:

bundles.CreateJs("test-bundle-2", "~/Js/Bundle2")
    .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
            .ForDebug(builder => builder
                .EnableCompositeProcessing()
                .SetCacheBusterType<MyCacheBuster>())
            .Build()
    );

您还可以看到此启动类的示例:https://github.com/Shazwazza/Smidge/blob/master/src/Smidge.Web/Startup.cs#L126


2
投票

有关调试的Rendering docs中有一节,其中包括完整性:

默认情况下,Smidge将组合/压缩/缩小,但在开发过程中,您可能不希望这种情况发生。上述每种渲染方法都有一个可选的布尔“debug”参数。如果将此值设置为true,则禁用combine / compress / minify。

它继续包含一个如何使用ASP.NET Core MVC的环境Tag Helper来管理它的示例:

<environment names="Development">
    <script src="my-awesome-js-bundle" type="text/javascript" debug="true"></script>
</environment>
<environment names="Staging,Production">
    <script src="my-awesome-js-bundle" type="text/javascript"></script>
</environment>

SmidgeConfig直接从IConfiguration获得版本,可以在source中看到:

public string Version => _config["version"] ?? "1";

这意味着您无法在代码本身内更改它,但您可以向ASP.NET Core配置系统添加一些内容,以便为此提供不同的值。

编辑:我更多地调查了配置事项并得出结论,你可以用AddInMemoryCollection达到你想要的效果。 docs给出了如何使用它的一个很好的例子,所以我在下面为你提供了一个特定于上下文的例子,改编自示例代码:

var dict = new Dictionary<string, string>
{
    {"smidge:version", "1"}
};

var builder = new ConfigurationBuilder();

// ...
// Whatever code you're using to set up the builder.
// If you're in ASP.NET Core 2, this will be setup differently, but the docs cover it well.
// ...

builder.AddInMemoryCollection(dict);
© www.soinside.com 2019 - 2024. All rights reserved.