动态设置使用ASP.NET的CSS值 [英] Dynamically setting CSS values using ASP.NET

查看:166
本文介绍了动态设置使用ASP.NET的CSS值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在哪里的图像和其他资源将位于从该网站的主要内容一个单独的域的网站。我们将使用类似'www.example.com'的主站点,然后'images.example.com为所有风格额外资源等。

I'm working on a site where the images and other resources will be located on a separate domain from the main content of the site. We will use something like 'www.example.com' for the main site, and then 'images.example.com' for all extra resources for styles, etc.

在开发网站,我将保留所有这些资源在本地开发。机器。这里的挑战是保持CSS引用生产服务器和开发环境之间是一致的。

When developing the site I will keep all of these resources on local dev. machines. The challenge here is keeping CSS references consistent between the production server and development environments.

我在想什么做的是创造一个的web.config ,将存储图像服务器的URL关键。然后,从开发转换到生产的时候我可能只是改变web.config中值,一切都将完成。

What I was thinking of doing was creating a web.config key that would store the URL of the images server. Then, when switching from development to production I could just change the web.config value and everything would be done.

有没有办法将值添加到一个CSS文件,动态或以其他方式,从在配置或C#类的某个地方?或者,我要对这个错误的方式?

Is there any way to add a value to a CSS file, dynamically or otherwise, from some place in a config or C# class? Or am I going about this the wrong way?

此外,我有限的使用.NET 2.0,如果有差别。

Also, I'm limited to using .NET 2.0 if that makes a difference.

更新

要在此展开多一点,我知道我可以使用一个web.config设置服务器控件的URL。那些已经在动态生成的。什么我更感兴趣的是哪些选项我有修改(或做的的东西的),以静态的CSS文件,这将让我改变的URL的东西,如背景图像资源,将引用在CSS。有什么我能做的,除了查找/替换使用我的IDE的价值观?也许东西,可以自动部署脚本来完成?

UPDATE
To expand on this a little more, I know I can use a web.config setting for server controls' URLs. Those are already generated dynamically. What I'm more interested in is what options I have for modifying (or doing "something") to static CSS files that will allow me to change URLs for things such as background image resources that would be referenced in CSS. Is there anything I can do besides find/replacing the values using my IDE? Perhaps something that can be done automatically with a deployment script?

推荐答案

是保持镜像服务器的选项上的CSS文件?如果有可能,你可以把所有相关的图像引用,然后你只需要更新的链接的CSS文件。

Is keeping the CSS file on the image server an option? If that it possible, you could make all the image references relative, and then you just need to update the link to the css file.

<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings("css-server") %>style.css" />

如果你仍然想动态发送或生成CSS文件:

If you still want to send or generate a css file dynamically:

css文件不必在css中结束。 ASPX是罚款。你可以这样做:

css files don't have to end in css. aspx is fine. You could do this:

<link rel="stylesheet" href="style.aspx" />

然后在您的网页style.aspx:

and then in your style.aspx page:

protected void page_load(){
    Response.ContentType = "text/css";
    if (ConfigurationManager.AppSettings("css-server") == "local") {
        Server.Transfer("css/local.css");
    } else {
        Server.Transfer("css/production.css");
    }   
}

如果你仍然想动态生成CSS文件,我会使用一个HttpHandler,设置的contentType为text / CSS,然后生成的Response.Write的CSS。如果你坚持有CSS中的页尾,你总是可以注册的CSS进去IIS到ASP.NET,然后在Global.asax中application_Begin要求传入的请求,该文件中的.css结尾,使用httpcontext.current.rewritepath你的处理程序。

If you still want to dynamically generate a css file, I'd use an HttpHandler, set the contenttype to "text/css", then generate the css with Response.Write. If you insist on having the page end in css, you could always register css to go to asp.net in IIS, then on incoming requests in global.asax application_Begin request, if the file ends in .css, use httpcontext.current.rewritepath to your handler.

这将有style.css中的净效应在运行时被动态生成。

This will have a net effect of style.css being dynamically generated at runtime.

这篇关于动态设置使用ASP.NET的CSS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆