[英]Bundling CSS and Scripts in Web Forms application
我在這里關於將我的CSS和腳本捆綁到我的Web窗體應用程序。
首先,我想指出我正在關注本教程: http : //blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-網絡forms.aspx
我已經在App_Start類BundleConfig中看起來像這樣:
using System.Web;
using System.Web.Optimization;
namespace SitePessoal.Web
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery/core/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery/core/jquery-ui-{version}.js"));
bundles.Add(new StyleBundle("~/CSS/css").Include(
"~/CSS/Estilos.css",
"~/CSS/Print.css",
"~/CSS/Styles.css"));
}
}
}
另外,我已經使用Nugget下載了Optimization包,之后我轉到了我的Global.asax文件並試圖在Application_Start方法中注冊它,如下所示:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
BundleTable.EnableOptimizations = true;
}
不幸的是,這是事情不起作用的地方。 不幸的是,它一直用紅色強調課程,並給我以下消息:
Error 3 The name 'BundleTable' does not exist in the current context
Error 4 The name 'BundleTable' does not exist in the current context
Error 2 The name 'BundleConfig' does not exist in the current context
關於為什么會發生這種情況的任何想法? 提前致謝!
最好的祝福,
狂
我解決了@MadGatsu遇到同樣教程后遇到的問題。
@Pricey是正確的,%MadGatsu正確添加了對Global.asax的引用,包括,
<%@ Import Namespace="System.Web.Optimization" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
但還有一點。
基於缺乏Global.asax.cs ,我推斷出我們有網站 。 在這種情況下,我們需要將Bundleconfig.cs文件放在特殊的aspnet文件夾App_Code中 。 不要添加App_Start文件夾來包含該文件,因為它不起作用。 簡單地移動我的BundleConfig.cs從App_Start到的app_code糾正了“BundleConfig”不會在目前情況下誤差存在 。
你可能已經解決了這個問題,但以防它對任何人都有用..你可能需要在你的Global.asax.cs
添加對System.Web.Optimization
和BundleConfig
類命名空間SitePessoal.Web
的引用。
例:
using System.Web.Optimization;
using SitePessoal.Web;
namespace SitePessoal.Web.Application
{
public class Global : HttpApplication
{
private void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
BundleTable.EnableOptimizations = true;
}
}
}
您可能還需要將System.Web.Optimization
命名空間添加到web.config
的頁面(如果尚未這樣做),因為僅僅添加對Microsoft.AspNet.Web.Optimization
包的引用是不夠的。通過NuGet
。
<pages>
<namespaces>
<add namespace="System.Web.Optimization"/>
</namespaces>
</pages>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.