2014-03-02 24 views
2

我的角應用程序是由ASP.NET webapi支持的,我在那裏提供了一個index.html和angular處理其他所有內容。我想使用捆綁,但我看不到我會怎麼做。我是否必須使用剃鬚刀(或webforms)才能引用包?或者是否有一個選項可以讓捆綁輸出的固定名稱可以在我的src/hrefs中引用?在正則html中的ASP.NET包?

爲了澄清,我沒有使用MVC或Webforms來提供html。您只需將其重定向到index.html,並且路由都是客戶端。我的包配置是使用WebActivator.PostApplicationStartMethod完成的。

+0

所以你使用普通的HTML(無asp.net-MVC),讓你的數據從web api? – shenku

+0

@shenku - 在原來的帖子中澄清 –

+0

@GeorgeR你找到一個合適的解決方案嗎?我處於相同的情況,並且我無法處理捆綁刷新(即,當我在js文件中進行更改並重建項目時,捆綁不會刷新) –

回答

1

首先,只回答這個問題,你可以使用一個簡單的鏈接在HTML文件中

<script src='bundles/mybundle' type='text/javascript' language='text/javascript'></script> 

=>將包括你的JavaScript捆綁到網頁

的問題,你將有使用這種方法是,如果您修改包中包含的* .js文件,修改將不會顯示在包中。 這是關於「bundle cache busting」的所有內容,ASP.NET的一個很好的功能,但只能用於剃刀模板... 顯然,您也可以重新啓動池(但這很慢且很難自動化):)

要解決這個問題,你可以用下面的

using System.Linq; 
using System.Web.Mvc; 
using System.Web.Optimization; 

namespace Controllers 
{ 
    public class DebugBundlesController : Controller 
    { 
     // GET: debugbundles/mybundle 
     public ActionResult Mybundle() 
     { 
      return InlineBundleJavaScript("~/bundles/mybundle"); 
     } 

     private ActionResult InlineBundleJavaScript(string bundlePath) 
     { 
      //see https://blog.mariusschulz.com/2015/10/25/inlining-css-and-javascript-bundles-with-asp-net-mvc 
      var bundleContext = new BundleContext(HttpContext, BundleTable.Bundles, "~/bundles"); 
      var bundle = BundleTable.Bundles.Single(b => b.Path == bundlePath); 
      var js = bundle.GenerateBundleResponse(bundleContext).Content; 
      return JavaScript(js); 
     } 
    } 
} 

定義自己的ASP.NET MVC控制器和您使用,如:

<script src='debugbundles/mybundle' type='text/javascript' language='text/javascript'></script> 

=>現在,每次你做一個變化,捆綁將會重新生成特德。

注意,因爲你刪除束幾乎所有的好處只有在開發過程中使用這個(即特定的客戶端和服務器緩存)