2

I have seen many questions and answers about this; but none of the answers seem to give me a direction here.

The following code works fine when I specify the virtual path as an address to a physical file:

bundles.Add(new Bundle("~/Modules/SIRVA.Connect.Intake.SAP/Content/style.css") 
    .Include("~/Modules/Intake/Content/Style.css"));

However, if I specify a virtual path that does not exist in the file system (like so...):

bundles.Add(new Bundle("~/content/intake.css")
    .Include("~/Modules/Intake/Content/Style.css"));

... MVC will render the the style but when you click on the href it displays, "Page not found," and the styles don't work (because they aren't there).

<link href="/content/intake.css" rel="stylesheet"/>

MVC's own examples appear to point to a non-existent physical location as the virtual path, but it works fine when they do it!

bundles.Add(new StyleBundle("~/Content/css").Include(
    "~/Content/bootstrap.css",
    "~/Content/site.css"
    ));

Can anyone tell me why I have to set the virtual path of the Bundle() constructor to a physical file location?

user1477388
  • 20,790
  • 32
  • 144
  • 264

1 Answers1

3

The path isn't supposed to be a physical file location. Can you try and specify a directory instead of a specific file type? Like this (using StyleBundle because I get the idea that you're wanting to create a css bundle):

bundles.Add(new StyleBundle("~/bundles/css")
    .Include("~/Modules/Intake/Content/Style.css"));

This will allow the application to make a query string when in production for version control purposes in a production environment (if your bundles are enabled to minimize and compress).

<link rel="stylesheet" href="/bundles/css?v=f-rOZpG8nqcdBI9IS1kiTRlij7Eim7N9U1_RJYwd4_w1"></link>

I believe that using the physical path for a file in bundle works in a limited way, like it won't allow you to add more content there because it's already in use. I haven't really used it in that way, so I don't know what the exact result is.

Edit (Just in case):

You will also need to render it properly in your view. Here's an example (razor view):

@Styles.Render("~/bundles/css")
Brittany
  • 96
  • 1
  • 9
  • That renders `` in the page and when I click the link (or browse in Chrome -> *F12* -> Sources) it is 404 not found. I might be able to solve this if I could just find some documentation on how the virtual path is supposed to work. Everything I find isn't low level enough; i.e. I can't understand how it works under the hood. – user1477388 Nov 13 '14 at 14:42
  • @user1477388 That is what it should be rendering. Basically, all of the relative paths to your stylesheets that you include in .Include("style1.css", "style2.css") will be mashed together into one file, so that your application doesn't have to call each one separately (potentially slowing down your page load time). It also provides the options of minifying and compressing that file. Are the files that you're listing in .Include() pointing to existing stylesheets? Also, did you add `BundleConfig.RegisterBundles(BundleTable.Bundles);` to your Application_Start or BundleConfig? – Brittany Nov 13 '14 at 17:10
  • 2
    @user1477388 Also, looks like a few people were getting 404s from their bundles because they were missing some information in their Web.config [here](http://stackoverflow.com/questions/19769662/bundled-css-link-gets-a-404-error). I only use `` and it works fine for me. – Brittany Nov 13 '14 at 17:55
  • Thanks for asking the question, "[did you register your bundles in] your Application_Start or BundleConfig?" My app uses a lot of different assemblies and dependency injection, so I had to create a custom `ActionFilterAttribute` and register it so I could register tenant-specific bundles. Mine is a multi-tenant app and each tenant can have their own bundle configs. Thanks for your help. – user1477388 Nov 13 '14 at 18:16