C# Visual Studio 2012条件绑定

C# Visual Studio 2012条件绑定,c#,asp.net,css,visual-studio-2012,bundling-and-minification,C#,Asp.net,Css,Visual Studio 2012,Bundling And Minification,我刚开始与VS 2012 RC合作。我已经创建了一个带有母版页和单个web表单的测试站点。目前,我正在使用此代码捆绑站点上的整个style文件夹: Global.asax BundleTable.Bundles.EnableDefaultBundles(); Site.master <link rel="stylesheet" type="text/css" href="Styles/css" /> 我的建议是: 转到Global.asax。确保方法应用程序\u Start包含以

我刚开始与VS 2012 RC合作。我已经创建了一个带有母版页和单个web表单的测试站点。目前,我正在使用此代码捆绑站点上的整个
style
文件夹:

Global.asax

BundleTable.Bundles.EnableDefaultBundles();
Site.master

<link rel="stylesheet" type="text/css" href="Styles/css" />
我的建议是:

转到
Global.asax
。确保方法
应用程序\u Start
包含以下行:

protected void Application_Start()
{
    ...
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
查找或创建类
BundleConfig
,如下所示,最好是在文件夹
App\u 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"));

    }
}
现在在每个适当的页面中使用相应的捆绑包:

<link rel="stylesheet" type="text/css" href="Styles/page1" />
(这是
cshtml
,但
aspx
语法肯定非常相似)

请注意,每页必须有一个单独的包。您不应该动态修改同一个捆绑包。捆绑包具有虚拟URL。在您的示例中,它只是
css
。这些都是由浏览器缓存的,因此无论您是否在运行中更改了bundle的内容,浏览器都可能认为这是相同的,因此不会重新获取它


如果您不想注意将每个页面手动添加到上述方法中。你可以把它自动化。以下代码可以让您了解如何:

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;
    }
}
用法:

<link rel="stylesheet" type="text/css" href="<%: MyStyleHelper.RenderPageSpecificStyle(Page.AppRelativeVirtualPath) %>" />


如果您只在master中加载site.css,然后加载一个页面css文件,那么为什么要捆绑?@TimBJames,我正在加载整个文件夹的内容。我想在母版页中只加载站点级CSS,然后根据需要绑定其他CSS文件。这不是违背了捆绑销售的目的吗?毕竟,如果不进行绑定,您将在第一次加载时加载site.css和page1.css,然后在进入第2页时,它将只加载第3页的page2.css和page3.css,等等。假设特定于页面的css文件通常比一般的站点文件小得多,那么通过绑定,您将在每个页面中加载不同的大文件,如果不进行捆绑,您将在每页中加载一个小文件,唯一的改进是在第一页加载1个文件而不是2个文件(总大小相同)。我错了,还是您的自动化示例假设这是一个MVC应用程序?嗨,James Hill,是的,我的第一个快速草稿是MVC。它是用记事本编码的,也有一些bug。现在我把它改写成普通ASP并进行了测试。它似乎工作得很好。请参阅我的编辑。仅供参考-
ResolveUrl()
应添加到您的使用中。没有它就不行。
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;
    }
}
<link rel="stylesheet" type="text/css" href="<%: MyStyleHelper.RenderPageSpecificStyle(Page.AppRelativeVirtualPath) %>" />