Asp.net-Mvc

文件夾路徑中帶有萬用字元的 ASP.NET MVC 4 捆綁包

  • August 22, 2013

我的團隊使用自定義 NuGet 包來安裝 jQuery UI,它將主題文件放入如下目錄結構中:

  • 內容

    • jquery-ui-1.10.3

      • 圖片
      • jquery-ui.css
      • jquery-ui.min.css

我正在嘗試使用 ASP.NET MVC 4 捆綁包將此內容包含在我的應用程序的 App_Start 文件夾中的 BundleConfig 類中,如下所示:

bundles.Add( new StyleBundle( "~/bundles/css" )
                .Include( "~/Content/normalize-{version}.css",
                          "~/Content/jquery-ui-{version}/jquery-ui.css",
                          "~/Content/Site.css" ) );

當我執行該站點時,這會引發錯誤:

Directory does not exist.
Parameter name: directoryVirtualPath

我也試過:

bundles.Add( new StyleBundle( "~/bundles/css" )
                .Include( "~/Content/normalize-{version}.css" )
                .IncludeDirectory( "~/Content/jquery-ui-*", "*.css" )
                .Include( "~/Content/Site.css" ) );

這也不起作用(顯然)。我可以明確指定文件夾上的版本,但這會破壞使用捆綁包的部分好處。

那麼如何在文件夾路徑中使用萬用字元呢?

您可以使用IncludeDirectory搜尋子目錄的重載版本。

假設您有以下文件:

\Root\Content\jquery-ui-1.10.3\jquery-ui.css

使用此程式碼查找並添加它:

.IncludeDirectory("~/Content", "jquery-ui.css", true)

這很有用,因為它總能找到jquery-ui.css,無論你把它放在哪裡。

這種方法的缺點是它將搜尋並包含它找到的所有 jquery-ui.css文件,如果您不確保只jquery-ui.css存在一個文件,這可能會導致一些錯誤。

(記住搜尋子目錄也會搜尋根目錄,即~/Content)

引用自:https://stackoverflow.com/questions/18368493