2017-08-27 72 views
0

我有一個空的mvc模板與Angular客戶端(普通html/js/css)與following結構。
爲了實現捆綁和縮小,我做了this文章中的步驟。 我RegisterBundles方法有以下規則:ASP.NET Bundle AngularJS

public static void RegisterBundles(BundleCollection bundles) 
     { 
      //bundles.Add(new ScriptBundle("~/bundles/js").IncludeDirectory(
      // "/App/","*.js", true)); 
      bundles.Add(new StyleBundle("~/bundles/styles.css").Include(
       "/Content/*.css")); 

      bundles.Add(new ScriptBundle("~/bundles/app.js").Include(
       "~/App/app.modules.js") 
       .IncludeDirectory("~/App/Components/", "*Module.js", true) 
       .IncludeDirectory("~/App/Components/", "*Service.js", true) 
       .IncludeDirectory("~/App/Components/", "*Controller.js", true)); 
     } 

要連結這些包,我要把它放到的Index.html

<link href="/bundles/styles.css" rel="stylesheet"/> 
<script src="/bundles/app.js" type="text/javascript" language="text/javascript"></script> 

更新

我更新了重寫規則在web.config文件

<system.webServer> 
     <httpErrors errorMode="Detailed" /> 
     <asp scriptErrorSentToBrowser="true"/> 
     <rewrite> 
      <rules> 
      <rule name="AngularJS Routes" stopProcessing="true"> 
       <match url=".*" /> 
       <conditions logicalGrouping="MatchAll"> 
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
       <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> 
       <add input="{REQUEST_URI}" negate="true" pattern="^/bundles/styles$" ignoreCase="true"/> 
       <add input="{REQUEST_URI}" negate="true" pattern="^/bundles/app$" ignoreCase="true"/> 
       </conditions> 
       <action type="Rewrite" url="/" /> 
      </rule> 
      </rules> 
     </rewrite> 
     </system.webServer> 

而我仍然沒有得到捆綁。我得到404找不到和this

+0

是否有這裏涉及任何重寫規則?雖然看起來你的'.js'文件中包含'html'內容,例如,當你詢問'app.js'時,服務器可能返回'index.html'頁面。 –

+0

@KirkLarkin是的,有。更新了主題。有沒有解決這個問題? – Vendor

+0

看看這個[補充答案](https://stackoverflow.com/questions/22345420/bundling-and-minification-without-asp-net-mvc/28738462#28738462)到捆綁你引用的帖子。另外,你還記得禁用調試模式嗎? –

回答

0

看來,我沒有IIS(IIS => Your website => Modules)中的「Bundle module」,並且在角度項目中也存在WebGrease包的問題。因此,解決辦法是:
1.在包管理器控制檯執行:Update-package webgrease
2.在web.config確保有節:

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="BundleModule"/> 
     <add name="BundleModule" type="System.Web.Optimization.BundleModule"/> 
    </modules> 
</system.webServer>