C# ASP.NET MVC中的多个CSS文件包
我正在用ASP.NETMVC构建这个网站,基本上会有一个默认的外观。 根据进入门户的机构,颜色、横幅和问候语将发生变化 例如: www.portal.com/Institution1 颜色:蓝色 横幅:照片1 www.portal.com/Institution2 颜色:绿色 横幅:照片2 我试图在BundleConfig.cs文件中实现这一点,但尚未找到解决方案 如果有人使用www.portal.com进入门户,我将使用:C# ASP.NET MVC中的多个CSS文件包,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我正在用ASP.NETMVC构建这个网站,基本上会有一个默认的外观。 根据进入门户的机构,颜色、横幅和问候语将发生变化 例如: www.portal.com/Institution1 颜色:蓝色 横幅:照片1 www.portal.com/Institution2 颜色:绿色 横幅:照片2 我试图在BundleConfig.cs文件中实现这一点,但尚未找到解决方案 如果有人使用www.portal.com进入门户,我将使用: bundles.Add(new StyleBundle("~/Cont
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"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Institution2/Institution2.css",
"~/Content/site.css"));
如果有人使用www.portal.com/Institution2进入门户,我想使用:
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Institution1/Institution1.css",
"~/Content/site.css"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Institution2/Institution2.css",
"~/Content/site.css"));
有什么方法可以做到这一点吗?没有一种具体的方法可以做到这一点。几年前我也做过类似的事情。您可以定义一组主题,并将用户或组织与已知主题关联。捆绑包名称可以像标识符一样使用。如果您以可管理的方式布局内容,则可以将用户或组织与从任何位置提取内容的样式或主题相关联。对用户进行常规身份验证后,您可以在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"));
}
我会这样做:
@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"));
}
是的,除非用户访问相关页面,否则不要将这些捆绑包包含到页面中。上面的代码只是声明bundle是可用的,实际上并没有导致它们被使用。这是在布局和/或查看页面中完成的。你把注意力放在了错误的代码上。