Bundling scripts are not getting rendered

2019-01-12 09:35发布

I am having a problem with script bundling and minification with ASP .NET I have tried all popular solution found on internet but still having the same problem.

My BundleConfig.cs looks like

namespace MYPROJ{
public class BundleConfig
{
    public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
    {
        if (ignoreList == null)
            return;
        ignoreList.Ignore("*.intellisense.js");
        ignoreList.Ignore("*-vsdoc.js");
        ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
        ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
        ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();
        AddDefaultIgnorePatterns(bundles.IgnoreList);

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

…
…

        //bundles.Add(new ScriptBundle("~/Scripts").Include("~/Scripts/jquery.unobtrusive-ajax.min.js", "~/Scripts/kendoui/kendo.all.min.js", "~/Scripts/kendoui/kendo.combobox.min.js", "~/Scripts/kendoui/kendo.grid.min.js"));
        //bundles.Add(new ScriptBundle("~/Scripts").Include("~/Scripts/kendoui/kendo.all.min"));
        //bundles.Add(new ScriptBundle("~/Scripts").Include("~/Scripts/kendoui/kendo.combobox.min"));
        //bundles.Add(new ScriptBundle("~/Scripts").Include("~/Scripts/kendoui/kendo.grid.min.js"));            
        bundles.Add(new ScriptBundle("~/Scripts").IncludeDirectory("~/Scripts", "*.js").IncludeDirectory("~/Scripts/kendoui", "*.js"));

        ……
        BundleTable.EnableOptimizations = true;
    }
}
}

And in master view:

@Scripts.Render("~/Scripts")

Now after all this when I runs I get this tag:

<script src="/Scripts?v=ZnxC8dcoc3fJ-hfKQHLiTe19PjJFHwPhwHPUncuBtzE1"></script>

And upon using chrome code inspector I found out that the status code for the above resource is Status Code: 302 Found And for Scripts/ it is Status Code: 404 Not Found

And I also cannot access the script file by clicking it in the view source, so looks like nothing is loaded however all files and directories are rightly placed. Just for the info, my styleSheets bundling is working fine.

Kindly help Thank you.

4条回答
迷人小祖宗
2楼-- · 2019-01-12 09:56

The bundle module logic that decides whether or not to handle a request, will not takeover requests to existing files or directories. So that's why your bundle requests don't work when they live at the same virtual path as an existing directory (or file).

查看更多
仙女界的扛把子
3楼-- · 2019-01-12 09:58

Have you tried using non existing directory in Bundle's virtual path? Like

...new ScriptBundle("~/bundle/Scripts")...
查看更多
Viruses.
4楼-- · 2019-01-12 10:03

The accepted answer did not work for me. I use MVC 4.0 on Visual Studio 2010 SP1. I had a js file named "jquery-ui.min.js" file and was not getting loaded. My Bundle.config code was:

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(                        
                            "~/Scripts/jquery-ui.min.js"));

I renamed the file to jquery-ui.js and updated my code as

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(                        
                        "~/Scripts/jquery-ui.js"));

I got this information from here.

查看更多
SAY GOODBYE
5楼-- · 2019-01-12 10:04

You can't give your bundle a name that is also the name of an existing directory. Rename the bundle or add a /js to make it work correctly:

bundles.Add(new ScriptBundle("~/Scripts/js").IncludeDirectory("~/Scripts", "*.js").IncludeDirectory("~/Scripts/kendoui", "*.js"));

and

@Scripts.Render("~/Scripts/js")

Any other name that doesn't exist would work as well, e.g.

 bundles.Add(new ScriptBundle("~/ScriptMonkey").IncludeDirectory("~/Scripts", "*.js").IncludeDirectory("~/Scripts/kendoui", "*.js"));

...assuming you don't have /ScriptMonkey directory.

查看更多
登录 后发表回答