C# Script.Render(绝对或部分路径)未禁用编译绑定debug=true
您是否知道,当包路径与根文件夹不相关时,Script.Render为什么不禁用包进行编译debug=“true” 我使用根目录的相对路径创建捆绑包,如(此路径类型是必需的,否则将引发异常): 但当我试图呈现它时,我需要提供完整的url,如下所示:C# Script.Render(绝对或部分路径)未禁用编译绑定debug=true,c#,asp.net,asp.net-mvc,system.web.optimization,C#,Asp.net,Asp.net Mvc,System.web.optimization,您是否知道,当包路径与根文件夹不相关时,Script.Render为什么不禁用包进行编译debug=“true” 我使用根目录的相对路径创建捆绑包,如(此路径类型是必需的,否则将引发异常): 但当我试图呈现它时,我需要提供完整的url,如下所示: Scripts.Render("http://myserver/bundles/myscripts/") 而无论编译调试模式如何,都会启用捆绑包。 你知道我遗漏了什么吗 我的问题与—我正在以这种方式呈现我的包—现在非常相关:如何在编译debug=“t
Scripts.Render("http://myserver/bundles/myscripts/")
而无论编译调试模式如何,都会启用捆绑包。
你知道我遗漏了什么吗
我的问题与—我正在以这种方式呈现我的包—现在非常相关:如何在编译debug=“true”时禁用它
有什么想法吗
谢谢!
Ovi回答我自己的问题:脚本。如果捆绑url作为完整url提供,则Render不会根据编译模式切换捆绑,如:
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);
}
public MvcHtmlString BundleScript(字符串bundleUrl)
{
var javascriptBuilder=新的StringBuilder();
bool filesExist=false;
bool isDynamicEnabled=isDynamicEnabled();
如果(!isDynamicEnabled)
{
IEnumerable fileUrls=GetBundleFileCollection(bundleUrl);
字符串rootVirtualDirectory=“~/content/js/”;
if(fileurl!=null)
{
foreach(fileUrl中的字符串fileUrl)
{
Append(newscripttag().WithSource(GetScriptName(fileUrl,rootVirtualDirectory)).ToHtmlString());
}
fileexist=true;
}
}
如果(isDynamicEnabled | |!FileExist)
{
Append(newscripttag().WithSource(bundleUrl.ToHtmlString());
}
返回MvcHtmlString.Create(javascriptBuilder.ToString());
}
私有IEnumerable GetBundleFileCollection(字符串bundleVirtualPath)
{
var collection=new BundleCollection{BundleTable.Bundles.GetBundleFor(bundleVirtualPath)};
var bundleResolver=新的bundleResolver(集合);
返回bundleResolver.GetBundleContents(bundleVirtualPath);
}
私有bool已启用()
{
返回BundleTable.EnableOptimizations;
}
私有静态字符串GetScriptName(字符串scriptUrl、字符串virtualDirectory)
{
返回scriptUrl.Replace(virtualDirectory,string.Empty);
}
当您尝试提供相对路径时,您的代码是什么?您好,jbbi,当我在应用程序中创建包时,\u Start是问题的第一行代码,当我尝试渲染时,它类似于
。我之所以使用这种语法,是因为捆绑包要经过CDN,我需要完整的URL。但无论如何,一旦我在Scripts.Render中不使用~字符,我就无法再使用编译调试模式禁用捆绑包。这是我的问题,我如何自定义此行为,以便能够使用部分路径或完整URL作为路径,并且仍然使用编译调试模式来决定是否启用绑定。您不是在类似于BundleTable.EnableOptimizations=true;的地方强制它吗?Ondrej,不,我不使用EnableOptimizations=true。
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);
}