繁体   English   中英

ASP.net Rejuicer动态组合js

[英]ASP.net Rejuicer combine js dynamically

我正在寻找使用Rejuicer http://rejuice.me/将我的ASP.net MVC网站的所有JS文件合并为一个JS文件。 这可以正常工作,但是由于所有配置都在Application_Start()事件处理程序中进行了设置,因此我不允许添加或删除JS文件,而不必也重置/回收应用程序。

有谁对我如何解决这个问题有任何想法。 还是有更好的替代品Rejuicer?

在Application_Start中加载配置是一个折衷方案。 另一种选择是挂接到FileSystemWatcher并等待文件夹更改。 考虑到Web应用程序的性质,这不是理想的选择。 一些替代使用web.config也会强制重置。

有诸如Workbench之类的工具在开发过程中(在VS.NET中)完成工作。 这消除了对应用程序启动周期的依赖,并且在应用程序启动时文件在那里。

在_layout.cshtml文件中,我有与此类似的代码。 可以在此处定义站点的js文件列表,而不必重置或重新编译站点。

@{

    //combine all JS files into one file using Rejuicer. 
    List<string> JsFiles = new List<string>();

    //define list of JS files here
    JsFiles.Add("~/Scripts/One.js");
    JsFiles.Add("~/Scripts/Two.js");      


    if (System.Web.HttpContext.Current.Application["JsFileListing"] != null)
    {

        var JsFileRejuiced = (List<string>)System.Web.HttpContext.Current.Application["JsFileListing"];

        //check if we have any new/removed/renamed files
        if (JsFiles.Except(JsFileRejuiced).Count()> 0 || JsFileRejuiced.Except(JsFiles).Count() > 0)          
        {
            //force application reset
            HttpRuntime.UnloadAppDomain();                
        }
    }

    if (System.Web.HttpContext.Current.Application["JsFileListing"] == null)
    {

        var Combiner = Rejuicer.OnRequest.ForJs("~/Combined.js").Combine;

        foreach (var file in JsFiles)
        {
            Combiner.File(file);
        }
        Combiner.Configure();

        System.Web.HttpContext.Current.Application["JsFileListing"] = JsFiles;
    }
}
@Rejuicer.Rejuiced.JsFor("~/Combined.js")

暂无
暂无

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

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