【问题标题】:Multiple bundles of CSS files in ASP.NET MVCASP.NET MVC 中的多个 CSS 文件包
【发布时间】:2017-12-04 20:50:55
【问题描述】:

我正在用 ASP.Net MVC 构建这个网站,基本上会有一个默认外观。 根据进入门户的机构,颜色、横幅和问候语会发生变化。

示例: www.portal.com/Institution1

颜色:蓝色

横幅:照片 1

www.portal.com/Institution2

颜色:绿色

横幅:照片 2

我正在尝试在 BundleConfig.cs 文件中完成此操作,但我没有找到解决方案。

如果有人使用 www.portal.com 进入门户,我将使用:

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

如果有人使用 www.portal.com/Institution1 进入门户,我想使用:

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

如果有人使用 www.portal.com/Institution2 进入门户,我想使用:

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

有没有办法做到这一点?

【问题讨论】:

  • 是的,除非用户访问相关页面,否则不要将这些捆绑包包含到页面中。上面的代码只是将包声明为可用,实际上并没有导致它们被使用。这是在布局和/或视图页面中完成的。您专注于错误的代码。

标签: c# asp.net asp.net-mvc asp.net-mvc-5


【解决方案1】:

没有一种特定的方法可以做到这一点。几年前我做过类似的事情。您可以定义一组主题并将用户或组织与已知主题相关联。捆绑包名称可以用作标识符。如果您以可管理的方式布局您的内容,那么您可以将用户或组织与您从任何地方提取内容的样式或主题相关联。在对用户进行一般身份验证后,您可以在 IndexModel 或其他任何内容上设置一个属性,以便在主页加载时您可以使用以下内容访问所需的样式:

@Styles.Render(@Model.CurrentSyle.BundleName) 

我会这样做:

public static void RegisterBundles(BundleCollection bundles)
{
    //perhaps something like.
    foreach (MyTheme theme in ThemeController.SelectAll())
       AddThemeStyleBundle(bundles, theme.BundleName, theme.ThemeName);
}

private static void AddThemeStyleBundle(BundleCollection bundles,string bundleName, string themeName)
    bundles.Add(new StyleImagePathBundle(bundlename).Include(
        "~/Content/bootstrap.css", 
        String.Format("~/Content/{0}/Institution.css",themeName), 
        "~/Content/site.css"));
}

【讨论】:

    猜你喜欢
    • 2012-12-09
    • 2016-07-12
    • 1970-01-01
    • 2011-02-07
    • 2011-09-14
    • 2013-02-04
    • 2011-03-19
    • 2011-10-17
    • 2019-01-29
    相关资源
    最近更新 更多