C# MVC Bundle JS CSS

Nuget Command
Install-Package Microsoft.AspNet.Web.Optimization

新增App_Start\BundleConfig.cs
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
//JS
bundles.Add(new ScriptBundle("~/Scripts/jquery")
                             .Include("~/Scripts/jquery-{version}.js"));

//CSS
bundles.Add(new StyleBundle("~/Content/jquery_ui")
                             .Include("~/Content/themes/base/*.css"));
}
}

編輯global.asax.cs
using System.Web.Optimization;

protected void Application_Start() {
     ...
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     ...

}

編輯Views/web.config
<system.web.webPages.razor>
    <host>
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization" />
        <add namespace="Conference" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

_Layout.cshtml
<!--CSS-->
@Styles.Render("~/Content/bootstrap")
<!--JS-->
@Scripts.Render("~/Scripts/bootstrap")
document.write('<script src="@BundleTable.Bundles.ResolveBundleUrl("~/Scripts/jquery")">' + '<' + '/script>');

留言

這個網誌中的熱門文章

Android WebView 發生跑版

Entity Delete 因為實體未公開其關聯性的外部索引鍵屬性而發生錯誤

OS X Yosemite 10.10.1 + Apache + PHP + MariaDB