我如何获得System.Web.Optimization捆绑使用自定义文件夹的工作的IIS虚拟目录? [英] How do I get System.Web.Optimization bundles to work with custom folders in an IIS virtual directory?

查看:196
本文介绍了我如何获得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.

从这个帖子,<一个href="http://stackoverflow.com/questions/11371710/is-it-possible-to-unit-test-bundleconfig-in-mvc4">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.

再有就是这个帖子,<一个href="http://stackoverflow.com/questions/9647585/why-does-resolvebundleurl-not-work-for-custom-folders/9647592#comment16170422_9647592">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?

12年8月27日

问题回答说:基本上System.Web.Optimization不适用于网络的网址是子IIS虚拟目录的工作

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

问题是这些线路的code里面BundleResolver.GetBundleContents

The problem are these lines of code inside BundleResolver.GetBundleContents

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

这基本上假定被捆绑将在初级web应用体育文件夹下一个体育夹的每一个文件。

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

这个问题,国际海事组织,是正在搜索的网页相对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裸code,然后再重新编译,所以我可以修复了。第一步是到RelativePath属性添加到BundleItem,一个额外的构造函数BundleItem传承下去源相对URL路径,preserve什么网页相对搜索目录文件夹了。然后,我更换了code以上的循环之前,基本上试图复赛与他们的BundleItem发现的文件,以便它们可以被转换回一个有效的网页网址

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.

我真的认为asp.net团队应该在IIS虚拟目录System.Web.Optimizations支持文件。特别是现在的VS2012具有IIS防爆preSS支持,这将最终使人们更方便建立与文件模块化的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.

请参阅我的答案在这里了解如何将其注册信息:<一href="http://stackoverflow.com/questions/28499885/bundletable-bundles-getbundlefor-returns-but-not-items-inside">BundleTable.Bundles.GetBundleFor()回报,但里面没有项目

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天全站免登陆