简体   繁体   English

Visual Studio 2012条件捆绑

[英]Visual Studio 2012 Conditional Bundling

I just began working with VS 2012 RC. 我刚开始使用VS 2012 RC。 I've created a test site with a master page and a single web form. 我创建了一个包含母版页和单个Web表单的测试站点。 Currently, I'm using this code to bundle the entire Styles folder on the site: 目前,我正在使用此代码捆绑网站上的整个Styles文件夹:

Global.asax Global.asax中

BundleTable.Bundles.EnableDefaultBundles();

Site.master 的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. 问题:测试站点有一个站点级CSS文件,用于控制站点的整体外观。 In addition to the site-level CSS, each page could have their own CSS definitions. 除了站点级CSS之外,每个页面都可以有自己的CSS定义。 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? 是否可以在母版页中仅包含site.css文件,然后在每个页面需要时有条件地将.css文件添加到软件包中?

I tried this in the code behind of Default.aspx but it didn't work: 我在Default.aspx后面的代码中尝试了这个但是它不起作用:

BundleTable.Bundles.Add(new Bundle("~/Styles/Default.css"));

My suggestion: 我的建议:

Goto Global.asax . 转到Global.asax Ensure the method Application_Start contains following line: 确保Application_Start方法包含以下行:

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

Find or create class BundleConfig as follows, preferrably in folder App_Start : 按如下方式查找或创建类BundleConfig ,最好在文件夹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: 现在在每个适当的页面中使用相应的bundle:

<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). (这是cshtml ,但aspx语法肯定非常相似)。

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 . 在你的例子中它只是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. 这些都是由浏览器缓存的,因此无论天气如何,您都可以动态更改bundle的内容,浏览器可能会认为这是相同的,并且不会重新获取它。


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) %>" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM