ASP.Net MVC dynamic script bundle does not load and gives 404 error first few times

Viewed 134

I am working on an existing ASP.Net MVC web application on .Net 4.5.2 and MVC 5.2. This has dynamic script bundles on 250 odd views which are added to the BundleTable only when the particular view is first accessed. This leads to an issue where the first time any view is hit, the script bundle returns a 404 error. But refreshing the page few times resolves the issue - at which point the bundle loads up fine and then gets cached on the browser.

The code for this looks like:

//View:
@model XYZ
@{ ViewBag.Title = "Title"; }
@section JavaScript {
    @Html.RenderScripts("~/Scripts/a.js", "~/Scripts/b.js")
}
// HtmlHelper RenderScripts extension
public static IHtmlString RenderScripts(this HtmlHelper helper, bool isMinified, params string[] filePaths)
{
    var path = (helper.ViewDataContainer as WebPageBase).VirtualPath.Replace("~/", "~/Scripts/").Replace(".cshtml", "").Replace("../", "");
    var bundle = new ScriptBundle(path).Include(filePaths);
    bundle.Orderer = _orderer;
    if (!isMinified) bundle.Transforms.Clear(); 
    BundleTable.Bundles.Add(bundle);
    return Scripts.Render(path);
}

It appears that the 1st request might be adding the bundle, but there's a lag in generating and serving up the bundle itself which is resulting in the 404 error. So how soon can a bundle be used once it's added - does it need a warm up?

Is this a wrong pattern of using Script bundles? Is there a way to address this so I can continue to declare the view-specific script bundles on the view without moving them to the bundle config?

0 Answers
Related