2014-02-27 46 views
0

您是否有任何人知道为什么Script.Render没有禁用编译的bundle debug =“true”不是相对于根文件夹Script.Render(绝对路径或部分路径)未禁用编译绑定debug = true

我创建使用像根目录的相对路径管束(此路径类型是强制性的,否则将引发异常):

Bundle jsBundle = new ScriptBundle("~/bundles/myscripts/"); 

但是,当我尝试呈现它,我需要提供完整的网址,如下所示:

Scripts.Render("http://myserver/bundles/myscripts/") 

而且的束不论编译调试模式启用。

任何想法我失踪?

我的问题与this question非常相关 - 我正在渲染我的包 - 现在:编译debug =“true”时如何禁用它?

任何想法?

谢谢! 的Ovi

+0

当您尝试提供相对路径时,您的代码是什么? – binard

+0

嗨jbbi,当我在Application_Start中创建包时,是从问题开始的第一行代码,当我尝试渲染它时,就像'<%:Scripts.Render(“protocol:// myserver/bundles/vxscripts /” )%>'。我使用这种语法,因为这个包通过e CDN,我需要完整的URL。 –

+0

但无论如何,一旦我在Scripts.Render中不使用〜字符,就无法使用编译调试模式禁用该包。这是我的问题,如何自定义此行为以便能够使用路径的部分路径或完整URL,并仍然使用编译调试模式来决定是否启用绑定。 –

回答

1

要回答我自己的问题:Scripts.Render不切换依赖于编译模式捆绑,如果束URL为提供完整的URL:

Scripts.Render("http://myserver/bundles/myscripts/") 

我采取的方法是创建我自己的mvc帮手来渲染包:

public MvcHtmlString BundleScript(string bundleUrl) 
{ 
     var javascriptBuilder = new StringBuilder(); 
     bool filesExist = false; 
     bool isDynamicEnabled = IsDynamicEnabled(); 

     if (!isDynamicEnabled) 
     { 
      IEnumerable<string> fileUrls = GetBundleFilesCollection(bundleUrl); 
      string rootVirtualDirectory = "~/content/js/"; 

      if (fileUrls != null) 
      { 
       foreach (string fileUrl in fileUrls) 
       { 
        javascriptBuilder.Append(new ScriptTag().WithSource(GetScriptName(fileUrl, rootVirtualDirectory)).ToHtmlString()); 
       } 
       filesExist = true; 
      } 
     } 

     if (isDynamicEnabled || !filesExist) 
     { 
      javascriptBuilder.Append(new ScriptTag().WithSource(bundleUrl).ToHtmlString()); 
     } 
     return MvcHtmlString.Create(javascriptBuilder.ToString()); 
} 

private IEnumerable<string> GetBundleFilesCollection(string bundleVirtualPath) 
{ 
     var collection = new BundleCollection { BundleTable.Bundles.GetBundleFor(bundleVirtualPath) }; 
     var bundleResolver = new BundleResolver(collection); 
     return bundleResolver.GetBundleContents(bundleVirtualPath); 
} 

private bool IsDynamicEnabled() 
{ 
     return BundleTable.EnableOptimizations; 
} 
private static string GetScriptName(string scriptUrl, string virtualDirectory) 
{ 
     return scriptUrl.Replace(virtualDirectory, string.Empty); 
}