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

时间:2014-02-23 19:02:22

标签: .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)
{    
    //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**

我可能缺少配置要求吗?我的路径是否格式错误?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我弄清楚问题是什么。在使用CDN时,捆绑收集似乎不喜欢include目录的规范。一旦我删除了删除的其他包含语句,问题就消失了。

<强> BEFORE

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*"));
}

<强> AFTER

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时,似乎不要包括任何本地文件路径。

答案 1 :(得分:0)

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

bundles.Add(new ScriptBundle("~/Resources/Scripts/js/jquery", jqueryCdnPath

可以更改为

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