如何防止捆绑使用未包含在项目中的文件?

时间:2023-01-13 13:27:12

I have an asp.net mvc 5 project using bundling like so:

我有一个使用捆绑的asp.net mvc 5项目,如下所示:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));
}

I have one version of jqueryui included in my project, version 1.11.0. However, I also have an older version (1.8.11) on disk in the scripts folder, but not included in the project.

我的项目版本1.11.0中包含一个版本的jqueryui。但是,我在脚本文件夹中的磁盘上也有一个旧版本(1.8.11),但未包含在项目中。

如何防止捆绑使用未包含在项目中的文件?

When I call @Scripts.Render("~/bundles/jqueryui"), it renders like this (including both the files).

当我调用@ Scripts.Render(“〜/ bundles / jqueryui”)时,它会像这样渲染(包括两个文件)。

<script src="/Scripts/jquery-ui-1.11.0.js"></script>
<script src="/Scripts/jquery-ui-1.8.11.js"></script>

Needless to say this causes multiple issues (this happens with css files as well). Obviously I could simply delete the old files, and I do; but I find myself doing that quite frequently, since another branch of the TFS repository has these old files. Every time we merge they come over again.

毋庸置疑,这会导致多个问题(这也会发生在css文件中)。显然我可以简单地删除旧文件,我这样做;但我发现自己经常这样做,因为TFS存储库的另一个分支有这些旧文件。每次我们合并他们都会再次来。

How can I instruct the bundling engine to ignore files that are not part of the solution?

如何指示捆绑引擎忽略不属于解决方案的文件?

2 个解决方案

#1


3  

You cannot ignore files that are part of the solution because when the web app is running there is no solution file. The publish wizard will not deploy files that are not part of the solution. But if you already have multiple files deployed they will all be rendered. I've created this method that grab only the latest one:

您无法忽略属于解决方案的文件,因为当Web应用程序运行时,没有解决方案文件。发布向导不会部署不属于解决方案的文件。但是如果你已经部署了多个文件,那么它们都将被渲染。我创建了这个方法,只抓取最新的方法:

    public static string[] GetLatestVersion(params string[] files)
    {
        System.Collections.Generic.List<string> latestFiles = new System.Collections.Generic.List<string>();
        foreach (var file in files)
        {
            var folder = System.IO.Path.GetDirectoryName(file);
            var phisicalFolder = System.Web.HttpContext.Current.Server.MapPath(folder);
            var pattern = System.IO.Path.GetFileName(file).Replace("{version}", "*");
            var virtualFile = folder.Replace("\\","/") + "/" + System.IO.Path.GetFileName(System.Linq.Enumerable.First(System.Linq.Enumerable.OrderByDescending(System.IO.Directory.GetFiles(phisicalFolder, pattern), x => x)));
            latestFiles.Add(virtualFile);
        }
        return latestFiles.ToArray();
    }

    public static void RegisterBundles(BundleCollection bundles)
    {

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    GetLatestVersion("~/Scripts/modernizr-{version}.js")));
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    GetLatestVersion("~/Scripts/jquery-*.js")));
    }

#2


1  

set version in bundle :

在bundle中设置版本:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-1.11.0.js"));
}

#1


3  

You cannot ignore files that are part of the solution because when the web app is running there is no solution file. The publish wizard will not deploy files that are not part of the solution. But if you already have multiple files deployed they will all be rendered. I've created this method that grab only the latest one:

您无法忽略属于解决方案的文件,因为当Web应用程序运行时,没有解决方案文件。发布向导不会部署不属于解决方案的文件。但是如果你已经部署了多个文件,那么它们都将被渲染。我创建了这个方法,只抓取最新的方法:

    public static string[] GetLatestVersion(params string[] files)
    {
        System.Collections.Generic.List<string> latestFiles = new System.Collections.Generic.List<string>();
        foreach (var file in files)
        {
            var folder = System.IO.Path.GetDirectoryName(file);
            var phisicalFolder = System.Web.HttpContext.Current.Server.MapPath(folder);
            var pattern = System.IO.Path.GetFileName(file).Replace("{version}", "*");
            var virtualFile = folder.Replace("\\","/") + "/" + System.IO.Path.GetFileName(System.Linq.Enumerable.First(System.Linq.Enumerable.OrderByDescending(System.IO.Directory.GetFiles(phisicalFolder, pattern), x => x)));
            latestFiles.Add(virtualFile);
        }
        return latestFiles.ToArray();
    }

    public static void RegisterBundles(BundleCollection bundles)
    {

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    GetLatestVersion("~/Scripts/modernizr-{version}.js")));
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    GetLatestVersion("~/Scripts/jquery-*.js")));
    }

#2


1  

set version in bundle :

在bundle中设置版本:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-1.11.0.js"));
}