简体   繁体   中英

ASP.net Rejuicer combine js dynamically

Im looking at using Rejuicer http://rejuice.me/ to combine all the JS files of my ASP.net MVC site into one JS file. This works ok but it doesnt allow me to add or remove JS files without having to also reset/recycle the application, as all the configuration is set in the Application_Start() event handler.

Does anyone have any ideas on how I can get around this. Or if there is a better alternative to Rejuicer?

Loading configuration in Application_Start is a trade off. Other option is to hook into FileSystemWatcher and wait for folder changes. Given the nature of a web application, this isn't ideal. Some alternative use web.config which also forces a reset.

There are tools like Workbench which does its thing during development (in VS.NET). This removes dependency on the application start cycle and files are there when the app starts up.

In the _layout.cshtml file I have code similar to this. The list of js files for the site can be defined here without having to reset or recompile the site.

@{

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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