How do I get System.Web.Optimization bundles to work with custom folders in an IIS virtual directory?

不羁岁月 提交于 2019-11-27 07:38:40

问题


I have an asp.net mvc4. I have modules that are deployed as subdirectories within this application using IIS Virtual Directories and I need to reference files in these modules. These module dlls are registering bundles. But the bundles are not generating anything into the html page.

From this post, is-it-possible-to-unit-test-bundleconfig-in-mvc4 , I see that internally the bundles are using Server.MapPath. So it seems like it should work.

I hooked BundleTable.MapPathMethod and even called Server.MapPath myself which does correctly resolve to the right physical directory. But it still won't render anything into the html page.

Then there was this post, why-does-resolvebundleurl-not-work-for-custom-folders , that mentioned an "AddDirectory" function for custom folders BUT this function is no longer available in the most recent optimization library.

I've also tried making use of the new "IncludeDirectory" method, but that didn't work either

ScriptBundle scriptBundle = new ScriptBundle("~/bundles/jquery");
scriptBundle.IncludeDirectory(basePath + "/Scripts/","jquery-1.*");
bundles.Add(scriptBundle);

Anything else I can try to make this work?


8/27/12

PROBLEM ANSWERED: Basically System.Web.Optimization doesn't work with web urls that are sub IIS virtual directories.

The problem are these lines of code inside BundleResolver.GetBundleContents

string mapPathMethod = this.MapPathMethod("~/");
if (!file.FullName.StartsWith(mapPathMethod, StringComparison.OrdinalIgnoreCase))

that basically assumes every single file being bundled will be in a PHYSICAL folder underneath the primary web application PHYSICAL folder.

The problem, IMO, is that the web relative url path being searched for files to include is being converted to a physical path very early on and all reference to what the relative url path used to get those physical files is thrown away.

So, to see if I could make this work, I had to decompile System.Web.Optimization to bare code and then recompile again so I could "fix" it. First step was to add a RelativePath property to BundleItem, an extra constructor to BundleItem to pass down the source relative url path to preserve what the web relative search directory folder was. Then I replaced the code above with the loop before that basically tries to rematch the files found with their BundleItem so that they can be converted back to a valid web url

foreach (BundleItem bundleItem in bundleFor.Items)
{
  if (file.FullName.StartsWith(bundleItem.Path, StringComparison.OrdinalIgnoreCase)){
    string str = file.FullName.Replace(bundleItem.Path,bundleItem.RelativePath);
    str = str.Replace('\\', '/');
    strs.Add(str);
    break;
  }
}

Now my bundles are properly rendering. NOTE however that I have not yet tested this hack fix for release or with optimizations or minifying on.

I really think the asp.net team should make System.Web.Optimizations support files in IIS virtual directories. Especially now that the VS2012 has support for IIS Express that will finally make it a lot easier to build modular web apps with files being referenced via IIS Virtual Directories


回答1:


AddDirectory has been renamed IncludeDirectory so you could still try that.




回答2:


You should be able to accomplish this using a virtualPathProvider if you configure it properly.

See my answer here for info on how to register it: BundleTable.Bundles.GetBundleFor() returns but not Items inside



来源:https://stackoverflow.com/questions/12100524/how-do-i-get-system-web-optimization-bundles-to-work-with-custom-folders-in-an-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!