目前,我网站上的所有页面都有一行如下所示:
<link rel="stylesheet" type="text/css" media="screen, projection" href="css/screenpro.css" />
screenpro.css 是一个看起来像这样的小文件:
@import url("reset.css");
@import url("master.css");
@import url("account.css");
@import url("slideshow.css");
@import url("enriched.css");
@import url("ie.css");
@import url("popup.css");
一切正常。然而,我只是写了一个小处理程序来生成一个动态的 css 文件来改变页面的背景颜色。我已经将它包含在我的一个页面中,如下所示:
<link rel="Stylesheet" href="ColorChange.ashx" />
但是,我很懒,我的网站有很多页面 - 我不想在每个页面上都添加这个新的导入。我试着把这个:
@import url("../ColorChange.ashx");
还有这个:
@import url("ColorChange.ashx");
在 screenpro.css 中,但它不起作用。我有什么办法可以将生成动态 css 的 .ashx 页面包含到我现有的 css 树中,还是我必须从每个页面手动链接它?
编辑:ColorChange.ashx 中有趣的部分
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/css";
String query = context.Request.UrlReferrer.Query;
Match match = Regex.Match(query, "bg=(?<bg>\\w{6})");
if (!match.Success) return;
context.Response.Write("\n.proaccount div.box, .proouter #pnlHaveResults div.box { background-color:#" + match.Groups[1] + ";}\n");
}
最佳答案
您发送的 header 是否正确?类似于:'内容类型:文本/css' 因为浏览器需要它来将您的响应识别为 CSS。
我用的 ASP 不多,但在 PHP 中你可以做你想做的事,只需先打印你的标题,然后再打印你的规则。
关于c# - .css 文件中的 html 样式链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6848278/