Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解ASP.NEt MVC脚本绑定路径规范_.net_Asp.net Mvc_Asp.net Web Api_Web Optimization - Fatal编程技术网

了解ASP.NEt MVC脚本绑定路径规范

了解ASP.NEt MVC脚本绑定路径规范,.net,asp.net-mvc,asp.net-web-api,web-optimization,.net,Asp.net Mvc,Asp.net Web Api,Web Optimization,因此,我正在学习如何在MVC中使用脚本绑定实用程序,我有一个问题——我不太了解如何为每个ScriptBundle对象构造虚拟路径。例如,当使用允许指定CDN的构造函数时,我有以下设置 在Global.asax中,我有以下调用 BundleConfig.RegisterBundles(BundleTable.Bundles); 然后在BundleConfig课上,我 public static void RegisterBundles(BundleCollection bundles) {

因此,我正在学习如何在MVC中使用脚本绑定实用程序,我有一个问题——我不太了解如何为每个ScriptBundle对象构造虚拟路径。例如,当使用允许指定CDN的构造函数时,我有以下设置

在Global.asax中,我有以下调用

  BundleConfig.RegisterBundles(BundleTable.Bundles);
然后在BundleConfig课上,我

public static void RegisterBundles(BundleCollection bundles)
{    
    //add link to jquery on the CDN
    const string jqueryCdnPath = @"http://code.jquery.com/jquery-2.1.0.min.js";
    const string jqueryUiCdnPath = @"http://code.jquery.com/ui/1.10.4/jquery-ui.min.js";
    const string jqueryColorCdnPath = @"http://code.jquery.com/color/jquery.color-2.1.2.min.js";
    var jqueryQunitCdnPath = @"http://code.jquery.com/qunit/qunit-1.14.0.js"; 

    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery", jqueryCdnPath)
       .Include("~/Resources/Scripts/js/jquery/jquery-{version}.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/ui", jqueryUiCdnPath)
       .Include("~/Resources/Scripts/js/jquery/ui/j-{version}/jquery-ui.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/color", jqueryColorCdnPath)
       .Include("~/Resources/Scripts/js/jquery/color/jquery.color-{version}.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/jqueryval")
         .Include("~/Resources/Scripts/js/jquery.unobtrusive*", "~/Resources/Scripts/js/jquery.validate*"));
}
然而,当我构建并运行我的页面时,我在第一次添加时收到以下错误消息

**Directory does not exist.
Parameter name: directoryVirtualPath
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.ArgumentException: Directory does not exist.
Parameter name: directoryVirtualPath

Source File: c:\######.Web\App_Start\BundleConfig.cs    Line: 30 

Stack Trace: 


[ArgumentException: Directory does not exist.
Parameter name: directoryVirtualPath]
   System.Web.Optimization.Bundle.Include(String virtualPath, IItemTransform[] transforms) +90
   Network.Web.BundleConfig.RegisterBundles(BundleCollection bundles) inc:\######.Web.Web\App_Start\BundleConfig.cs:30
   Network.Web.MvcApplication.Application_Start() in c:\######.Web\Global.asax.cs:27

[HttpException (0x80004005): Directory does not exist.
Parameter name: directoryVirtualPath]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9936761
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Directory does not exist.
Parameter name: directoryVirtualPath]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9915300
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254**
这里是否有我可能缺少的配置要求?我的路径是否格式错误


提前感谢您的帮助。

将捆绑包中的目录更改为不存在的目录

bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery", jqueryCdnPath
可以改成

bundles.Add(new ScriptBundle("~/BUNDLES/jquery", jqueryCdnPath)

我知道问题出在哪里了。在使用CDN时,bundlecollection似乎不喜欢包含目录的规范。一旦我删除了附加的include语句,问题就消失了

之前

public static void RegisterBundles(BundleCollection bundles)
{    
    //add link to jquery on the CDN
    const string jqueryCdnPath = @"http://code.jquery.com/jquery-2.1.0.min.js";
    const string jqueryUiCdnPath = @"http://code.jquery.com/ui/1.10.4/jquery-ui.min.js";
    const string jqueryColorCdnPath = @"http://code.jquery.com/color/jquery.color-2.1.2.min.js";
    var jqueryQunitCdnPath = @"http://code.jquery.com/qunit/qunit-1.14.0.js"; 

    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery", jqueryCdnPath)
       .Include("~/Resources/Scripts/js/jquery/jquery-{version}.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/ui", jqueryUiCdnPath)
       .Include("~/Resources/Scripts/js/jquery/ui/j-{version}/jquery-ui.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/color", jqueryColorCdnPath)
       .Include("~/Resources/Scripts/js/jquery/color/jquery.color-{version}.js"));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/jqueryval")
         .Include("~/Resources/Scripts/js/jquery.unobtrusive*", "~/Resources/Scripts/js/jquery.validate*"));
}
之后

public static void RegisterBundles(BundleCollection bundles)
{    
    //add link to jquery on the CDN
    const string jqueryCdnPath = @"http://code.jquery.com/jquery-2.1.0.min.js";
    const string jqueryUiCdnPath = @"http://code.jquery.com/ui/1.10.4/jquery-ui.min.js";
    const string jqueryColorCdnPath = @"http://code.jquery.com/color/jquery.color-2.1.2.min.js";
    var jqueryQunitCdnPath = @"http://code.jquery.com/qunit/qunit-1.14.0.js"; 

    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery", jqueryCdnPath));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/ui", jqueryUiCdnPath));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/color", jqueryColorCdnPath));
    bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery/jqueryval")
         .Include("~/Resources/Scripts/js/jquery.unobtrusive*", "~/Resources/Scripts/js/jquery.validate*"));
}

因此,当您指定要使用CDN时,似乎不必费心包括任何本地文件路径

是否启用了捆绑和缩小?通过在Web.config文件的compilation元素中设置debug属性的值,可以启用或禁用绑定和缩小。请尝试对绑定进行注释。逐个添加语句,并找到出现此错误的语句。CDN路径是正确的,当然其中一条语句引用了错误的本地路径。当我这样做时,只有第一条add语句通过,其他所有语句都失败。因此,我试图理解什么是我可能对第一个目录有权利,但对其他目录似乎不正确。我不确定我是否理解为什么我要将目录更改为不存在的目录。如果目录存在,MVC将尝试从现有目录获取该文件,但找不到该文件。所以你必须给它一个虚拟路径。哦!但如果它真的存在呢?那么MVC不应该使用现有的文件吗?我想我正在试图理解这到底应该如何工作,除了几篇博客之外,似乎没有太多关于它的内容,这些博客似乎都在发布相同的示例,而没有太多关于实际机制的内容。