简体   繁体   English

在Web窗体应用程序中捆绑CSS和脚本

[英]Bundling CSS and Scripts in Web Forms application

I am here on a pickle about bundling my CSS and Scripts to my Web Forms application. 我在这里关于将我的CSS和脚本捆绑到我的Web窗体应用程序。

First of all, I'd like to point out that I was following this tutorial: http://blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-web-forms.aspx 首先,我想指出我正在关注本教程: http//blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-网络forms.aspx

I have already in the App_Start the class BundleConfig that looks like this: 我已经在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"));
        }
    }
}

Also, I have downloaded the Optimization package with Nugget, so afterwards I went to my Global.asax file and tried to register this in the Application_Start method like this: 另外,我已经使用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;
}

Unfortunately, this is where things do not work. 不幸的是,这是事情不起作用的地方。 Unfortunately, it keeps underlining the class with red and giving me the following messages: 不幸的是,它一直用红色强调课程,并给我以下消息:

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 

Any ideas as to why this may be happening? 关于为什么会发生这种情况的任何想法? Thanks in advance! 提前致谢!

Best regards, 最好的祝福,
Mad

I solved the problem @MadGatsu encountered following the same tutorial. 我解决了@MadGatsu遇到同样教程后遇到的问题。

@Pricey is correct, and %MadGatsu correctly adds references to the Global.asax , including, @Pricey是正确的,%MadGatsu正确添加了对Global.asax的引用,包括,

<%@ Import Namespace="System.Web.Optimization" %>
<script RunAt="server">        
    void Application_Start(object sender, EventArgs e)
    {
          BundleConfig.RegisterBundles(BundleTable.Bundles);

But there is a little more to it. 但还有一点。

I deduce, based on the lack of Global.asax.cs , that we have Websites . 基于缺乏Global.asax.cs ,我推断出我们有网站 In this case, we need to put our Bundleconfig.cs file in the special aspnet folder, App_Code . 在这种情况下,我们需要将Bundleconfig.cs文件放在特殊的aspnet文件夹App_Code中 Do not add a App_Start folder to contain the file because it will not work. 不要添加App_Start文件夹来包含该文件,因为它不起作用。 Simply moving my BundleConfig.cs to App_Code from App_Start corrected the 'BundleConfig' does not exist in the current context error. 简单地移动我的BundleConfig.csApp_Start的app_code纠正了“BundleConfig”不会在目前情况下误差存在

You probably fixed this already but just in case it's of use to anyone.. you probably need to add a reference to System.Web.Optimization and your BundleConfig class namespace SitePessoal.Web to your Global.asax.cs 你可能已经解决了这个问题,但以防它对任何人都有用..你可能需要在你的Global.asax.cs添加对System.Web.OptimizationBundleConfig类命名空间SitePessoal.Web的引用。

Example: 例:

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;
        }
    }
}

You probably also need to add the System.Web.Optimization namespace to your pages in your web.config if you haven't done it already, because it is not enough just to add the reference to the Microsoft.AspNet.Web.Optimization package via NuGet . 您可能还需要将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM