如何让 System.Web.Optimization 包与 IIS 虚拟目录中的自定义文件夹一起使用? [英] How do I get System.Web.Optimization bundles to work with custom folders in an IIS virtual directory?

查看:25
本文介绍了如何让 System.Web.Optimization 包与 IIS 虚拟目录中的自定义文件夹一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp.net mvc4.我使用 IIS 虚拟目录将模块部署为该应用程序中的子目录,我需要引用这些模块中的文件.这些模块 dll 正在注册包.但是这些包没有在 html 页面中生成任何内容.

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.

从这篇文章中,is-it-possible-to-unit-test-bundleconfig-in-mvc4 ,我看到包在内部使用 Server.MapPath.所以看起来它应该可以工作.

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.

我迷上了 BundleTable.MapPathMethod,甚至自己调用了 Server.MapPath,它确实正确解析到正确的物理目录.但它仍然不会在 html 页面中呈现任何内容.

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.

然后是这个帖子,why-does-resolvebundleurl-not-work-for-custom-folders ,其中提到了自定义文件夹的AddDirectory"功能,但该功能在最新的优化库中不再可用.

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.

我也尝试过使用新的IncludeDirectory"方法,但也没有用

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

问题解答:基本上 System.Web.Optimization 不适用于作为子 IIS 虚拟目录的网址.

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

问题在于 BundleResolver.GetBundleContents 中的这些代码行

The problem are these lines of code inside BundleResolver.GetBundleContents

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

这基本上假设捆绑的每个文件都位于主 Web 应用程序 PHYSICAL 文件夹下的 PHYSICAL 文件夹中.

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

IMO 的问题是,正在搜索要包含的文件的 Web 相对 url 路径很早就被转换为物理路径,并且所有对用于获取这些物理文件的相对 url 路径的引用都被丢弃了.

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.

所以,为了看看我是否可以完成这项工作,我不得不将 System.Web.Optimization 反编译为裸代码,然后再次重新编译,以便我可以修复"它.第一步是向 BundleItem 添加一个 RelativePath 属性,这是 BundleItem 的一个额外构造函数,用于传递源相对 url 路径以保留 Web 相对搜索目录文件夹的内容.然后我用循环替换了上面的代码,然后基本上尝试重新匹配找到的文件与它们的 BundleItem 以便它们可以转换回有效的 web url

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;
  }
}

现在我的包可以正确渲染.但是请注意,我尚未针对发布或优化或缩小功能测试此 hack 修复程序.

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.

我真的认为 asp.net 团队应该在 IIS 虚拟目录中制作 System.Web.Optimizations 支持文件.尤其是现在 VS2012 支持 IIS Express,这最终将使构建模块化 Web 应用程序变得更加容易,并且通过 IIS 虚拟目录引用文件

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

推荐答案

如果配置正确,您应该能够使用 virtualPathProvider 完成此操作.

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

有关如何注册的信息,请在此处查看我的答案:BundleTable.Bundles.GetBundleFor() 返回但不返回里面的 Items

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

这篇关于如何让 System.Web.Optimization 包与 IIS 虚拟目录中的自定义文件夹一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆