Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

I have already in the App_Start the class BundleConfig that looks like this:

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:

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

like image 375
MadGatsu Avatar asked May 16 '13 10:05

MadGatsu


People also ask

What is bundling in web development?

Bundling is the process of resolving the web of dependencies and merging (or 'packaging') the files (or modules) into optimized bundles for the browser, with the goal of reducing the number of requests for files when a user visits a web page.

What is CSS bundling?

Bundling is the process of rolling up a number of distinct resources together into a single downloadable resource. For example, a bundle may consist of multiple JavaScript or CSS files you bring down to the local machine by making a single HTTP request to an ad hoc endpoint.

Can we use bundling and minification with ASP NET web forms like MVC?

Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)


1 Answers

I solved the problem @MadGatsu encountered following the same tutorial.

@Pricey is correct, and %MadGatsu correctly adds references to the Global.asax, including,

<%@ 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. In this case, we need to put our Bundleconfig.cs file in the special aspnet folder, App_Code. Do not add a App_Start folder to contain the file because it will not work. Simply moving my BundleConfig.cs to App_Code from App_Start corrected the 'BundleConfig' does not exist in the current context error.

like image 112
subsci Avatar answered Sep 28 '22 17:09

subsci