2016-12-30 16 views
2

我有像下面這樣的外部js和css的url。這些網址與主應用程序無關。如何在ASP.NET MVC中縮小和打包外部腳本和樣式表文件

http://localhost/media/10/custom.js, 
http://localhost/media/11/custom1.js, 
http://localhost/media/12/custom2.js, 
http://localhost/media/13/custom3.js, 
http://localhost/media/14/custom4.js 

我想要做的包,然後再縮小這些URL文件

Dim urls(5) as string 
urls(0)=http://localhost/media/10/custom.js 
urls(1)=http://localhost/media/11/custom1.js 
urls(2)=http://localhost/media/12/custom2.js 
urls(3)=http://localhost/media/10/custom3.js 
urls(4)=http://localhost/media/10/custom4.js 

BundleTable.Bundles.Add(New ScriptBundle("~/Content/js").Include(urls)) 
BundleTable.EnableOptimizaions=True 

我收到以下錯誤:

the URL '....' is not valid. Only application relativenURLs (~/url) are allowed.

我怎麼能捆綁外部文件?

+2

**只允許應用程序relativenURL(〜/ url)** – Shyju

回答

1

無法捆綁外部資源。它有沒有意義,根據MSDN Bundling and Minification

Bundling is a feature that makes it easy to combine or bundle multiple files into a single file. Because bundling combines multiple files into a single file, it reduces the number of requests to the server that is required to retrieve and display a web asset, such as a web page.

如果你是bunlde外部資源的bunlder具有下載它們(它並不能幫助您以最小的請求),並檢查資源的更新。

是的,你可以捆綁CDN資源,如果你去Announcing the Microsoft AJAX CDN你看:

When the browser requests the script file it will be automatically served by the CDN "edge cache" server that is closest to the end-user. This means:

  1. The request will be processed much faster than if it had to hit your web-server (making the end-user's page load much faster)

  2. You don't have to pay for the bandwidth of this file – since the file comes from our server we pay the bandwidth cost (saving you money)

  3. The script can be easily cached across multiple web-sites – which means it might not even need to be downloaded if the user has already hit a web-site that requested the file (and as such has it already in the browser's cache).

服務器不下載它們只是重定向到他們。它有所不同。

即使它提供了一個更多的優點,瀏覽器每個主機名有六個同時連接的限制。它可以通過使用cdn來緩解。因爲cdn將擁有與託管站點不同的主機名。

+0

我們可以將cdn url綁定。那麼我認爲我們可以捆綁這些外部js網址,因爲這些網址只是cdn。如果我錯了,請糾正我。 –

+0

我已經爲你編輯了答案。 –

+0

所以這些JS檔案文件是我們正在生成的只是cdns或不同?是否可以捆綁這些文件。我需要爲我的應用程序做這些捆綁,所以請給我一個解決方案。 –