简体   繁体   English

为ASP.NET Core配置Smidge库

[英]Configure the Smidge library for asp.net core

Scott Hanselman today blogged about Smidge . Scott Hanselman今天在博客上介绍了Smidge I think the library is pretty nice and I'm evaluating the library. 我认为图书馆很好,我正在评估图书馆。

I like the option to define logic for Debug and Production like in the sample: 我喜欢为示例中的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()

However I couldn't find out what defines Debug / Production . 但是我找不到定义Debug / Production Is there a way to tell the system when he's in debug and when he is in production mode? 有什么办法可以告诉系统他何时处于调试状态以及何时处于生产模式?

Also seems that the Version can only be defined in the config. 似乎版本只能在配置中定义。

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

Is there an option do define the version in the code? 是否可以在代码中定义版本?

There's a section in the Rendering docs on debugging, included here for completeness: Rendering文档中有关于调试的部分,出于完整性考虑,此处包含:

By default Smidge will combine/compress/minify but while you are developing you probably don't want this to happen. 默认情况下,Smidge将组合/压缩/最小化,但是在开发过程中,您可能不希望这种情况发生。 Each of the above rendering methods has an optional boolean 'debug' parameter. 上面的每种渲染方法都有一个可选的布尔“调试”参数。 If you set this to true then the combine/compress/minify is disabled. 如果将其设置为true,则禁用合并/压缩/缩小。

It goes on to include an example of how to manage this using ASP.NET Core MVC's environment Tag Helper: 继续包括一个示例,该示例说明如何使用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 obtains the version from IConfiguration directly, as can be seen in the source : SmidgeConfig直接从IConfiguration获取版本,如在源代码中所示

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

This means you can't change it within the code itself, but you might be able to add something to the ASP.NET Core configuration system in order to provide a different value for this. 这意味着您不能在代码本身内对其进行更改,但是您可以向ASP.NET Core配置系统中添加一些内容,以便为此提供不同的值。

EDIT: I looked into the configuration thing a bit more and concluded that you could achieve what you want with AddInMemoryCollection . 编辑:我进一步研究了配置问题,并得出结论,您可以使用AddInMemoryCollection实现AddInMemoryCollection The docs give a good example of how to use this, so I've included a context-specific example for you below, adapted from the example code: 这些文档为如何使用此代码提供了一个很好的示例,因此我在下面为您提供了一个特定于上下文的示例,该示例根据示例代码改编而成:

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);

Is there a way to tell the system when he's in debug and when he is in production mode? 有什么办法可以告诉系统他何时处于调试状态以及何时处于生产模式?

This is covered in the docs here: https://github.com/Shazwazza/Smidge/wiki/Rendering#debugging and it's based on the debug="true" attribute of the html tag. 此处的文档中对此进行了介绍: https : //github.com/Shazwazza/Smidge/wiki/Rendering#debugging ,它基于html标记的debug="true"属性。

Also seems that the Version can only be defined in the config. 似乎版本只能在配置中定义。

The Version is controlled in Smidge by the Smidge.Cache.ICacheBuster . 版本由Smidge.Cache.ICacheBusterSmidge.Cache.ICacheBuster There are currently 2 implementations of this: 当前有两种实现:

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

So it's possible to specify one of these or implement your own. 因此,可以指定其中之一或实现自己的。 If you implement your own, you need to add it to your container like: 如果实现自己的实现,则需要将其添加到您的容器中,例如:

services.AddSingleton<ICacheBuster, MyCacheBuster>();

Then you can specify options for your bundle (there are various ways to do this), for example: 然后,您可以为捆绑包指定选项(有多种方法可以执行此操作),例如:

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

You can also see this startup class for examples: https://github.com/Shazwazza/Smidge/blob/master/src/Smidge.Web/Startup.cs#L126 您还可以查看以下启动类作为示例: https : //github.com/Shazwazza/Smidge/blob/master/src/Smidge.Web/Startup.cs#L126

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM