Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:41:32+00:00 2026-06-08T11:41:32+00:00

I just began working with VS 2012 RC. I’ve created a test site with

  • 0

I just began working with VS 2012 RC. I’ve created a test site with a master page and a single web form. Currently, I’m using this code to bundle the entire Styles folder on the site:

Global.asax

BundleTable.Bundles.EnableDefaultBundles();

Site.master

<link rel="stylesheet" type="text/css" href="Styles/css" />

Question: The test site has a site-level CSS file that controls the overall look and feel of the site. In addition to the site-level CSS, each page could have their own CSS definitions. Is it possible to include only the site.css file in the master page, and then conditionally add .css files to the bundle as each page requires?

I tried this in the code behind of Default.aspx but it didn’t work:

BundleTable.Bundles.Add(new Bundle("~/Styles/Default.css"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:41:33+00:00Added an answer on June 8, 2026 at 11:41 am

    My suggestion:

    Goto Global.asax. Ensure the method Application_Start contains following line:

    protected void Application_Start()
    {
        ...
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    Find or create class BundleConfig as follows, preferrably in folder App_Start:

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            ...
    
            bundles.Add(new StyleBundle("~page1").Include(
                 "~/Styles/site.css",
                 "~/Styles/page1.css"));
    
            bundles.Add(new StyleBundle("~page2").Include(
                 "~/Styles/site.css",
                 "~/Styles/page2.css"));
    
            ...
    
            bundles.Add(new StyleBundle("~pageN").Include(
                 "~/Styles/site.css",
                 "~/Styles/pageN.css"));
    
        }
    }
    

    Now use corresponding bundle in every appropriate page:

    <link rel="stylesheet" type="text/css" href="Styles/page1" />
    

    Or better from code:

    @Styles.Render("~/Styles/page1")
    

    (this is cshtml, but aspx syntax is for sure very similar).

    Note that you must have a separate bundle per page. You should not modify one and the same bundle on the fly. Bundles have virtual Urls. In your example it is just css. These are cached by browsers, so regardless weather you have changed the content of bundle on the fly a browser might think this is the same and do not re-fetch it.


    If you do not want to take care about adding each and every page manually to the method above. You could automate it. Following code could give you an idea how:

    public class MyStyleHelper
    {
        public static string RenderPageSpecificStyle(string pagePath)
        {
            var pageName = GetPageName(pagePath);
            string bundleName = EnsureBundle(pageName);
            return bundleName;
        }
    
        public static string GetPageName(string pagePath)
        {
            string pageFileName = pagePath.Substring(pagePath.LastIndexOf('/'));
            string pageNameWithoutExtension = Path.GetFileNameWithoutExtension(pageFileName);
            return pageNameWithoutExtension;
        }
    
        public static string EnsureBundle(string pageName)
        {
            var bundleName = "~/styles/" + pageName;
            var bundle = BundleTable.Bundles.GetBundleFor(bundleName);
            if (bundle == null)
            {
                bundle = new StyleBundle(bundleName).Include(
                    "~/styles/site.css",
                    "~/styles/" + pageName + ".css");
                BundleTable.Bundles.Add(bundle);
            }
            return bundleName;
        }
    }
    

    Usage:

    <link rel="stylesheet" type="text/css" href="<%: MyStyleHelper.RenderPageSpecificStyle(Page.AppRelativeVirtualPath) %>" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just began working on a little twitter-app using tweepy. is there any kind
I just began using Visual Studio to work on a practice .NET project. I
Hey I just began working on a new project that requires, tab navigation, and
I have just begun using LaTeX. I am working in Windows and am using
I just installed Joomla 1.5 and as I am currently working on the website
I just recently began in C# development, and I was working on a forms-based
I'm working on constructing a table using only divs. I began creating the table
Just began working on a new 2011 Macbook Pro only to find ADB cannot
Currently I'm still using Xcode 3 for iPhone development and I've just recently pulled
I just began working with ASP.NET and I'm trying to bring with me some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.