4

I want to add Bundles into an existing ASP.NET MVC 4 (.NET 4.5) website that uses:

I attempted to follow these directions: https://gist.github.com/jkarsrud/5143239, and the CSS loaded fine before I started down the bundles path.

On page load it inserts the style reference:

<link href="/bundles/marketingcss" rel="stylesheet">

But a 404 error happens:

> GET http://localhost:20459/bundles/marketingcss 404 (Not Found) 

Here's what I've in code:

Web.Config

<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/bundles" />

Global.asax

<%@ Application Codebehind="Global.asax.cs" Inherits="MapCom.Global" Language="C#" %>

Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Security;
using System.Web.SessionState;
using Umbraco.Web;

namespace MapCom
{
    public class Global : UmbracoApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            base.OnApplicationStarted(sender, e);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

_Layout.cshtml

@Styles.Render("~/bundles/marketingcss");

BundleConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

namespace MapCom
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            BundleTable.EnableOptimizations = true;
            ///
            ///Marketing Site CSS Bundle
            ///
            bundles.Add(new StyleBundle("~/bundles/marketingcss")
                .Include("~/plugins/bootstrap/css/bootstrap.min.css")
                .Include("~/css/font-awesome.min.css")
                .Include("~/plugins/parallax-slider/css/parallax-slider.css")
                .Include("~/css/combinedStyles.min.css")
                .Include("~/plugins/ladda-buttons/css/ladda.min.css")
                .Include("~/plugins/ladda-buttons/css/custom-lada-btn.css"));
        }
    }
}

Any ideas?

cvocvo
  • 1,586
  • 2
  • 23
  • 38
  • 1
    Not sure if it still applies, but it at least *used* to be that bundling wouldn't work at all if you included files with ".min" in the filename. The output seems to indicate that `Styles.Render` thinks you're pointing to an actual file rather than a bundle - since otherwise, it would include a version query parameter when `EnableOptimizations` is on - i.e. `/bundles/marketingcss?v=...`. – JimmiTh Jun 04 '14 at 23:03
  • @JimmiTh that did help to fix some of the errors that were evident in the bundle after I got it going. Thanks! – cvocvo Jun 05 '14 at 14:47

3 Answers3

6

After much digging into Umbraco, I noticed that the project I'm working on is utilizing a class:

 public class ApplicationEventHandler : IApplicationEventHandler

Which is explained in more detail here: http://our.umbraco.org/documentation/Reference/Events/application-startup

But the gist is:

In order to bind to certain events in the Umbraco application you need to make these registrations during application startup. Based on the Umbraco version you are using there are various ways to hook in to the application starting. The higher the version you are using the more robust this becomes.

So Umbraco's overriding some of the ASP.NET start-up methods.

I solved my issue by adding a reference to System.Web.Optimization in ApplicationEventHandler.cs and moved my bundle registration to this method in that class:

public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
cvocvo
  • 1,586
  • 2
  • 23
  • 38
  • Umbraco is overriding a lot of ASP.NET features like: Membership (UmbracoMembershipProvider), current thread : http://our.umbraco.org/forum/developers/api-questions/11342-Umbraco-overriding-SystemThreadingThreadCurrentThreadCurrentCulture and other more. Just think at this "problem" from now on – Razvan Dumitru Oct 20 '14 at 07:29
  • either putting the call in `OnApplicationStarting` or `OnApplicationStarted` work for me once I had updated the web.config (see my answer) – jenson-button-event Dec 04 '14 at 19:49
  • i had the same issue to access font awesome fonts, for **other solutions** try these links which deals with `StyleBundle` **virtualpath**: [Link 1](http://www.mvccentral.net/story/details/articles/kahanu/stylebundle-403-error-solved) , [Link 2](http://forums.asp.net/t/1774324.aspx?MVC4+css+bundling+and+image+references), [Link 3](http://ericpanorel.net/2013/10/25/font-awesome-4-0-mvc-bundling-and-minification/), [Link 4](http://jameschambers.com/2014/08/adding-some-font-awesome-to-mvc-and-bootstrap/) , hope this helps someone. – Shaiju T Feb 25 '16 at 09:34
3

The ONLY thing that worked for me here was adding the path to the bundle to the following setting in web.config

My bundle Code

BundleTable.Bundles.Add(new ScriptBundle("~/opt/jscripts").Include(
                "~/Scripts/twitterFetcher.js"
                ));
            BundleTable.EnableOptimizations = true;

Umbraco default setting

<add key="umbracoReservedPaths" value="~/umbraco,~/install/" />

New setting

<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/opt/" />
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
  • Right -- you definitely need to add the reserved path, otherwise Umbraco routing will try and handle that instead of letting .NET handle it. – cvocvo Dec 04 '14 at 22:45
2

try the following suggestions:

1.Do not use keyword bundles for your css virtual path so just change it to something else, and use the keyword bundles for your scripts only. like this:

 bundles.Add(new StyleBundle("~/Content/marketingcss")

(Remember you need to make sure that if you have a file called marketingcss under content folder you should change the virtual path to something else, so the virtual path shouldn't duplicate the physical path)

2.in your _Layout.cshtml use the following line instead of yours:

 @Styles.Render(BundleTable.Bundles.ResolveBundleUrl("~/Content/marketingcss"))

3.if the above suggestions didn't work then try this one in your _Layout.cshtml instead:

<link href="@System.Web.Optimization.BundleTable.
               Bundles.ResolveBundleUrl("~/Content/marketingcss")"
               rel="stylesheet"
               type="text/css" /> 

updated

4.just saw one of our friend's comment about not to putting .min files in your bundle, which is correct so you must not include .min files in your budleConfig class, and just use the normal files like bootstarp.css instead of bootstrap.min.css and do the same for all other files, of course make sure you have the normal files first so not just change the names.

I hope it helps to fix your code

Ali
  • 2,574
  • 1
  • 17
  • 24
  • Unfortunately none of those items helped -- determined it was an Umbraco technicality causing the issue. – cvocvo Jun 05 '14 at 14:48
  • @cvocvo Could you please elaborate on the Umbraco technicality, I think i am having the same issues as you – Roooss Jul 17 '14 at 22:29