Javascript ASP.NET MVC5 I don';我不想合并CSS文件
我知道合并CSS文件可能会影响网站的性能,但在我的情况下,我有以下情况 我的ASP.NET应用程序有4个颜色主题Javascript ASP.NET MVC5 I don';我不想合并CSS文件,javascript,css,asp.net,iis,Javascript,Css,Asp.net,Iis,我知道合并CSS文件可能会影响网站的性能,但在我的情况下,我有以下情况 我的ASP.NET应用程序有4个颜色主题 违约 红色的 紫色的 绿色的 我写了一点javascript来启用或禁用网站上的样式表 $("link[href='/Content/Style.css']").removeAttr('disabled'); $("link[href='/Content/Purple.css']").attr('disabled', 'disabled'); 在本地调试项目时工作性能良好 问题是当
$("link[href='/Content/Style.css']").removeAttr('disabled');
$("link[href='/Content/Purple.css']").attr('disabled', 'disabled');
在本地调试项目时工作性能良好
问题是当我发布到IIS时
显然,IIS将CSS文件合并到一个文件中(见下文),而我的javascript无法工作,因为它找不到CSS链接
<link href="/Content/styles?v=LJz1gcJyVEK_Atj5S7zoyR1lIdMwZ5IJIZn8ff329wQ1" rel="stylesheet">
有没有办法告诉IIS不要合并CSS文件
或者你对如何实现这一点有什么更好的建议吗?在你的项目中,你应该有一个类似以下内容的
App\u Start/Bundling.cs
文件:
Bundle css = new StyleBundle("~/Content/styles");
css.IncludeDirectory("~/Content/", "*.css", true);
这将从content目录中获取所有CSS文件,并将它们捆绑到单个CSS文件中。这不仅可以将文件捆绑在一起,还可以缩小它们,因此值得将您的主题集成到其中,例如:
string[] themes = { "Default", "Red", "Purple", "Green" };
foreach (var themeName in themes)
{
Bundle cssTheme = new StyleBundle("~/Theme/" + themeName);
cssTheme.Include("~/Theme/" + themeName + ".css");
bundleCollection.Add(cssTheme );
}
这将为每个主题创建一个包。然后,在\u Layout.cshtml
中,您可以如下方式呈现捆绑包:
@{
string[] themes = { "Default", "Red", "Purple", "Green" };
}
@Styles.Render(themes.Select(theme => "~/Theme" + theme).ToArray())
呈现时,您将看到打印出以下HTML:
<link href="/Theme/Default" rel="stylesheet" type="text/css">
<link href="/Theme/Red" rel="stylesheet" type="text/css">
<link href="/Theme/Purple" rel="stylesheet" type="text/css">
<link href="/Theme/Green" rel="stylesheet" type="text/css">
请注意使用,因为在产品中ASP.NET会在主题名称的末尾附加一个唯一的查询字符串IIS不会合并CSS,ASP.NET会合并CSS。您需要查看
Bundling.cs
文件,并从样式包中删除必要的文件。您还需要更新布局以手动添加这些文件。您可以使用主题的颜色创建几个捆绑包。谢谢,这确实有效,我从捆绑包中删除了样式,并将它们硬编码到_布局中。@Morpheus您的意思是我为每个主题创建一个捆绑包。然后我使用Javascript启用/禁用捆绑包?@Christos312
$("link[href^='/Theme/Default']").removeAttr('disabled');
$("link[href^='/Theme/Purple']").attr('disabled', 'disabled');