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>');
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>');
留言
張貼留言