Control Freak
Control Freak

Reputation: 13213

CSS Bundles with Controller Route Source

I created a controller path to return a css file, which works, and returns Response.ContentType = "text/css".

Now I'm trying to put that URL in my bundles file, like this:

bundles.Add(new StyleBundle("~/Content/custom").Include(
  "~/CSS/Custom/1"
));

NOTE: /CSS/Custom/1 is a route that returns a text/css file.

In my view I have:

@Styles.Render("~/Content/custom")

When I build the project, the bundler returns this in my HTML:

<link href="/Content/custom?v=" rel="stylesheet"/>

When I view the files source, It's empty.

How do I get this to work?

Upvotes: 0

Views: 152

Answers (2)

Dave Bush
Dave Bush

Reputation: 2402

Out of the box, you can only bundle physical files. If you need the CSS to be dynamically generated for some reason, you might consider creating a custom bundle. But if it were me, I'd just leave this as a separate download.

Upvotes: 0

jimSampica
jimSampica

Reputation: 12410

Not sure why you'd want to do this

After taking a look at it in reflector, during the bundling process it will take the virtual path to the item and check to make sure the file exists. (Code below) So bundling without CDN absolutely requires a file, not an application route.

if ((this.VirtualPathProvider == null) || this.VirtualPathProvider.FileExists(virtualPath))
{
    base.Add(new BundleItem(virtualPath, transforms));
} 

CDNs never hit this line, they take a different path.

bundles.UseCdn = true;

bundles.Add(new StyleBundle("~/Content/custom", "/CSS/Custom/1"));

BundleTable.EnableOptimizations = true;

Upvotes: 2

Related Questions